Manually calling the destructor before deleting

auto obj = new Object;
obj->~Object();
delete obj;

      

I know this is unusual, but is this behavior determined? Could this cause any unexpected problems?

+3


source to share


3 answers


You can only do this if , you replace the destroyed object it points obj

to with a new object:

auto obj = new Object;
obj->~Object();

new (obj) Object();
delete obj;

      

Otherwise, you call Undefined Behavior.


You must understand that:



  • new

    calls operator new

    to get memory, and then calls the provided constructor to create the object
  • delete

    calls the object's destructor , then calls operator delete

    to return to memory.


EDIT: As Bo Persson pointed out, this is not a good idea unless you can provide exception guarantees

+8


source


This Undefined Behavior causes a double call to the object's destructor. You don't follow the rules and the compiler is allowed to do whatever it wants with your code. Just don't do it.



+1


source


If we strictly adhere to the standard language, your code is causing undefined behavior and will cause problems.

However, I would be very surprised if any platform runs into any problems with the following:

struct Foo {};

void testFoo()
{
   Foo* foo = new Foo();
   foo->~Foo();
   foo->~Foo();
   foo->~Foo();
   foo->~Foo();
   foo->~Foo();
   foo->~Foo();
   foo->~Foo();
   delete foo;
}

      

On the other hand, I would be very surprised if any platform can accomplish the following without issue.

struct Bar {std::string s;};

void testBar()
{
   Bar* bar = new Bar{"test"};
   bar->~Bar();
   delete bar;
}

      

0


source







All Articles