Reputation: 39881
The code below is:
#include <iostream>
using namespace std;
class A
{
public:
A() {}
~A()
{
cout << "in destructor" << endl;
}
};
void main()
{
A a;
a.~A();
}
has the following output:
in destructor
in destructor
Why my app does not crash if the destructed object is destructed again?
Upvotes: 0
Views: 162
Reputation: 23813
C++ Standard, section § 12.4 [destructors]
Once a destructor is invoked for an object, the object no longer exists; the behavior is undefined if the destructor is invoked for an object whose lifetime has ended ( 3.8 ). [ Example: if the destructor for an automatic object is explicitly invoked, and the block is subsequently left in a manner that would ordinarily invoke implicit destruction of the object, the behavior is undefined.
So your program has undefined behavior, it could crash now, later, never, earth could stop spinning etc... don't do it.
Note:
void main()
must be int main()
Upvotes: 6