cplusplus / CWG

Core Working Group
23 stars 7 forks source link

[expr.new] p27 The invocation of deallocation is observable even though there's no outer try-catch statement #534

Closed xmh0511 closed 4 months ago

xmh0511 commented 4 months ago

Full name of submitter (unless configured in github; will be published with the issue): Jim X

[expr.new] p27 says:

If any part of the object initialization described above61 terminates by throwing an exception and a suitable deallocation function can be found, the deallocation function is called to free the memory in which the object was being constructed, after which the exception continues to propagate in the context of the new-expression.

Consider this example:

#include <iostream>
#include <memory>
void operator delete(void* ptr) noexcept{
    std::cout<<"delete\n";
}
struct A{
    A(){
        throw 0;
    }
};
int main(){
    new A();
    // try{
    //     new A();  
    // }catch(...){
    //     std::cout<<"caught\n";
    // }
}

The major implementations do not invoke the deallocation, instead, the program is directly terminated. However, the wording says that the exception continues to propagate after the deallocation is called, that is, [except.handle] p9 should apply later.

If no matching handler is found, the function std​::​terminate is invoked; whether or not the stack is unwound before this invocation of std​::​terminate is implementation-defined ([except.terminate]).

If wrapping new A(); by a try-catch statement, then delete will be printed.

frederick-vs-ja commented 4 months ago

Is there any concern missing in the current resolution of CWG2566?

xmh0511 commented 4 months ago

Oh, I forgot that I had posted a similar issue.