https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101480
--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> --- As Jakub said the behavior is the same for malloc() since years. When you split the testcase like > cat t.C #include <stdlib.h> #include <assert.h> bool flag = false; class C { bool prev; public: C() : prev(flag) { flag = true; // #1 } ~C() { flag = prev; } }; void g(int* p) { delete p; } void f() { int* p; { C c; p = new int; } g(p); } int main(int, char**) { f(); } > cat t2.C #include <stdlib.h> #include <assert.h> extern bool flag; void* operator new(unsigned long size) { assert(flag); // #2 return malloc(size); } void operator delete(void *p) { free(p); } it works as 'flag' is then global and no longer local to the TU. So it might work to disable the new/delete/malloc/free optimization when we see a definition of those functions.