https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94314
Bug ID: 94314 Summary: [10 Regression] Optimizing mismatched new/delete pairs Product: gcc Version: 10.0 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: glisse at gcc dot gnu.org Target Milestone: --- (originally posted at https://gcc.gnu.org/legacy-ml/gcc-patches/2019-08/msg00276.html , I don't know if we will do something about it, but it seems worth documenting it in bugzilla) Now that we optimize class-specific operator new/delete pairs (but you could do the same with the global replacable ones as well): #include <stdio.h> int count = 0; struct A { __attribute__((malloc,noinline)) static void* operator new(unsigned long sz){++count;return ::operator new(sz);} static void operator delete(void* ptr){--count;::operator delete(ptr);} }; int main(){ delete new A; printf("%d\n",count); // Should print 0. } If we do not inline anything, we can remove the pair and nothing touches count. If we inline both new and delete, we can then remove the inner pair instead, count increases and decreases, fine. If we inline only one of them, and DCE the mismatched pair new/delete, we get something inconsistent (count is -1). This seems to indicate we should check that the new and delete match somehow...