Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton Destructors

Should Singleton objects that don't use instance/reference counters be considered memory leaks in C++?

Without a counter that calls for explicit deletion of the singleton instance when the count is zero, how does the object get deleted? Is it cleaned up by the OS when the application is terminated? What if that Singleton had allocated memory on the heap?

In a nutshell, do I have to call a Singelton's destructor or can I rely on it getting cleaned up when the application terminates?

like image 266
Bryan Marble Avatar asked Nov 07 '08 21:11

Bryan Marble


People also ask

Can a singleton class have destructor?

No, and in general objects in C++ are not given private destructors. Keep in mind that Singleton means that there is only one instance, and so it is construction, not destruction, that needs to be controlled / prevented.

Can you destroy a singleton object?

Longer answer: You cannot destroy a singleton, except you use a special Classloader. If you need to destroy it, you shouldn't use a singleton at all.

How do I delete an object in singleton?

And deletion of singleton class object would be allow only when the count is zero. To design C++ delete singleton instance, first, we need to make the singleton class destructor private, so, it can not be accessed from outside of the class. Hence, user cannot delete the singleton instance using the keyword “delete”.

How do I stop my singleton from breaking?

There are many ways to prevent Singleton pattern from Reflection API, but one of the best solutions is to throw a run-time exception in the constructor if the instance already exists. In this, we can not able to create a second instance.


2 Answers

As so often, "it depends". In any operating system worthy of the name, when your process exits, all memory and other resources used locally within the process WILL be released. You simply don't need to worry about that.

However, if your singleton is allocating resources with a lifetime outside it's own process (maybe a file, a named mutex, or something similar) then you do need to consider the appropriate cleanup.

RAII will help you here. If you have a scenario like this:

class Tempfile { Tempfile() {}; // creates a temporary file  virtual ~Tempfile(); // close AND DELETE the temporary file  };  Tempfile &singleton() {   static Tempfile t;   return t; } 

...then you can be reassured that your temporary file WILL be closed and deleted however your application exits. However, this is NOT thread-safe, and the order of object deletion may not be what you expect or require.

however, if your singleton is implemented like THIS

Tempfile &singleton() {   static Tempfile *t = NULL;   if (t == NULL)     t = new Tempfile();    return *t; } 

... then you have a different situation. The memory used by your tempfile will be reclaimed, but the file will NOT be deleted because the destructor will not be invoked.

like image 185
Roddy Avatar answered Sep 25 '22 00:09

Roddy


You can rely on it being cleaned up by the operating system.

That said, if you are in a garbage collected language with finalizers rather than destructors you may want to have a graceful shutdown procedure that can cleanly shutdown your singletons directly so they can free any critical resources in case there are using system resources that won't be correctly cleaned up by merely ending the application. This is because finalizers run on a sort of 'best effort' basis in most languages. On the other hand there a very very few resources that need this sort of reliability. file handles, memory, etc. all go back to the OS cleanly regardless.

If you are using a singleton that is lazily allocated (i.e. with a triple-check lock idiom) in a language like c++ with real destructors rather than finalizers, then you cannot rely on its destructor being invoked during program shutdown. If you are using a single static instance then the destructor will run after main completes at some point.

Regardless, when the process ends, all memory returns to the operating system.

like image 22
Edward Kmett Avatar answered Sep 26 '22 00:09

Edward Kmett