public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100588] New: Destroying delete shouldn't be called if constructor throws
@ 2021-05-13 15:08 richardpku at gmail dot com
  2021-05-13 15:26 ` [Bug c++/100588] " redi at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: richardpku at gmail dot com @ 2021-05-13 15:08 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-02-02 23:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-13 15:08 [Bug c++/100588] New: Destroying delete shouldn't be called if constructor throws richardpku at gmail dot com
2021-05-13 15:26 ` [Bug c++/100588] " redi at gcc dot gnu.org
2022-01-06 21:46 ` jason at gcc dot gnu.org
2022-01-08  2:03 ` cvs-commit at gcc dot gnu.org
2022-02-02 23:41 ` jason at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).