Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[expr.new] p26 Is the matching deallocation function guaranteed to be invoked when no matching handler can be found? CWG2566 #5390

Open
xmh0511 opened this issue Apr 12, 2022 · 3 comments
Labels
cwg Issue must be reviewed by CWG. not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking.

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Apr 12, 2022

[expr.new] p26 says

If any part of the object initialization described above 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. If no unambiguous matching deallocation function can be found, propagating the exception does not cause the object's memory to be freed.

This implies that the deallocation function is unconditionally invoked as long as a matching version can be found. It didn't talk about whether or not a handler is necessary to be found before invoking that function.

Consider this example:

#include <iostream>
struct A{};
struct C{
    void* operator new(std::size_t N,A){
        std::cout<<"malloc\n";
        return malloc(N);
    }
    void operator delete(void* ptr,A){
        std::cout<<"abc\n";
        free(ptr);
    }
    C(){
      throw 0;
    }
};
int main(){
   auto ptr = new(A{}) C;
}

From this point, the invocation of the matching deallocation is observable, however, no implementations guarantee that function will be called. We can only find that [except.handle] p9 says:

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

However, it is irrelevant here since stack unwinding only concerns destroying an object.

According to the implementations' behavior, we may add a condition for whether or not the matching deallocation function is called. Two suggestions here:

  • If any part of the object initialization described above terminates by throwing an exception and a suitable matching deallocation function can be found, the deallocation function is called to free the memory in which the object was being constructed if a matching handler can be found([except.handle]), after which the exception continues to propagate in the context of the new-expression.
  • If any part of the object initialization described above terminates by throwing an exception and a suitable matching 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. If no matching handler([except.handle]) is found, whether or not the deallocation function is called is implementation-defined.
@frederick-vs-ja
Copy link
Contributor

I think this is definitely non-editiorial. Could you report here?

@xmh0511
Copy link
Contributor Author

xmh0511 commented Apr 13, 2022

@frederick-vs-ja Thanks for your suggestion. I have posted it there.

@jensmaurer
Copy link
Member

CWG2566

@jensmaurer jensmaurer added cwg Issue must be reviewed by CWG. not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking. labels Apr 14, 2022
@jensmaurer jensmaurer changed the title [expr.new] p26 Is the matching deallocation function guaranteed to be invoked when no matching handler can be found? [expr.new] p26 Is the matching deallocation function guaranteed to be invoked when no matching handler can be found? CWG2566 Apr 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cwg Issue must be reviewed by CWG. not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking.
Projects
None yet
Development

No branches or pull requests

3 participants