Manually calling the destructor before deleting
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
callsoperator new
to get memory, and then calls the provided constructor to create the object -
delete
calls the object's destructor , then callsoperator 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 to share
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 to share