I have a singleton class, whose instance get initialized at global scope within the class's CPP file:
Singleton* Singleton::uniqueInstance = new Singleton();
Its header file looks like:
class Singleton {
public:
static Singleton& getInstance() { return *uniqueInstance; }
static bool destroyInstance() { delete uniqueInstance; }
private:
//...
//... typical singleton stuff
static Singleton* uniqueInstance;
}; // end of class Singleton
I noticed that its destructor dodn't get executed during program termination, thus I added a public static interface Singleton::destroyInstance()
, to be manually invoke by client code before the program exits, for instance deletion. This snippet is not the complete code, and assumed that there are other codes that dealing with thread safety issue. In this case, how can I make use of RAII to eliminate the need of introducing such an interface? Thanks for advice.