LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Singleton delete instance (https://www.linuxquestions.org/questions/programming-9/singleton-delete-instance-534447/)

Genar 03-04-2007 07:23 AM

Singleton delete instance
 
In a simple Singleton pattern written in C++ how is it possible to delete the instance? (Apparently this pattern is very simple but I have realized that, in a lot of implementations, the instance was not really deleted). As the user member "dmail" commented, he uses a "getinstance" function to get a reference (a pointer is also possible) to the instance created, then he also registers an "atexit" function to delete the instance.

But, does not exist another solution without registering an atexit function in order to delete the instance?
Thanks.

dmail 03-04-2007 09:34 AM

Actually the code I use has as a template parameter which is scope of the singleton, "user deletes" or "app exit". As it would sound, with app exit the class registers the atexit func and user deletes would leak memory if the user did not delete it. Have at look at loki's source code for more advanced scoping of singletons http://loki-lib.sourceforge.net/.


All times are GMT -5. The time now is 03:30 PM.