1) i dont get it. Why does it call itself recursively? Why does it not not free the memory on the heap?
2) Since the destructor is virtual and the *obj is of type derived why will the destructor of Singleton be called. it shld be the destructor of derived.
jack, I think you should look a bit more into OO in C++ -
1) If you delete your object, it attempts to delete itself, which will cause it to attempt to delete itself, which will cause it to delete itself, which will...
2) ALL destructors from the class hierarchy are automagically invoked, starting from the lowest (most derived) class destructor and ending at the base class destructor.
You don't get a loop there because the destructor isn't deleting itself.
remember delete calls the destructor. So if you delete yourself in the destructor, it will call the destructor, which will delete itself, which will call the dtor, which will delete, which will call the dtor, etc, etc.
i also realize that the infinite call takes place because of the static instance pointer. If the pointer has been declared non as auto than there is no issue of the infinite call.
The reason i think is because static pointers exist even before the object is created and has a life span till the after end of the program. Sine you call delete on instance it will then call the delete over and over again.