I am using classical singelton pattern in C++ for a class (using a private static pointer to class instance within the class). Now, I am unsure about the deallocation(release of resources) by this static pointer. I am not calling explicitly the "delete" on that private instance in my normal code which can invoke destructor. My intention was that it should get deallocated at program exit and should free its resources but I am not sure that it is doing it.
I try to simulate with following example (please ignore syntax issues as I formulated it here):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Singelton
{
private:
static Singleton* instance;
// Constructor
// Private data from other classes which should get free at program exit
public:
Singelton* getInstance()
{...}
// Destructor where I write code to free other pointers that this instance refers to from other classes
}
int main()
{
// some code
// if I comment out the following line, Will it still call destructor for Singleton??
delete Singleton::getInstance();
}
I have tried to see by having printf in destructors but it does not show them which I suppose means that it does not do deallocation automatically at end??
I was mainly interested in some implicit way to delete resources at the end of program execution.
Besides your suggestions, I found http://www.research.ibm.com/designpatterns/pubs/ph-jun96.txt which is from the book you mentioned about how to kill singelton.