Destructor being called twice when being explicitly invoked

Solution 1:

It happens because you told it to happen. The destructor for an automatic variable is always called when the variable goes out of scope. You also called it. That's two calls total.

Calling an object's destructor does not signal to C++ not to call it again, since in normal execution there is no need to keep track.

The solution is to never manually call your destructor.

Solution 2:

Calling the destructor does not free the object.

The destructor is there to clean up the internals of the object and then the object itsself is freed after the destructor finishes.

It's an error to do what you are doing similarly to the way that you can call delete twice on an object but it's an error to do so.

There are only a very few cases where you want to call the destructor manually and this isn't one of them. It's really there for the times you manually construct an object at a memory location using placement new and then need to be able to destruct it without freeing the memory.