https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100588

            Bug ID: 100588
           Summary: Destroying delete shouldn't be called if constructor
                    throws
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: richardpku at gmail dot com
  Target Milestone: ---

Consider this program:


#include <cstdio>
#include <new>

class A {
 public:
  A() { throw 42; }
  ~A() { puts("A::~A"); }

  void operator delete(void* p) {
    puts("regular delete invoked");
    ::operator delete(p);
  }

  void operator delete(A* p, std::destroying_delete_t) {
    puts("destroying delete invoked");
    p->~A();
    ::operator delete(p);
  }
};

int main() {
  try {
    new A;
  } catch (int) {
  }
}


Output compiled with GCC:

destroying delete invoked
A::~A


Output compiled with Clang:

regular delete invoked


I believe clang has the correct behavior.

A destroying delete function is expected to invoke the destructor explicitly. 
As a result, the current GCC implementation would indirectly invoke the
destructor on an instance that wasn't successfully constructed, which obviously
isn't the intended behavior.

It is even worse if class A has a base class, in which case the destructor of
the base class is called twice if an exception is thrown in A's constructor.

Reply via email to