This code works very well, except for one problem I am having. As you can see the destructor is private because I don''t want anyone to delete the instance of the class. However, the destructor has some crucial code in it, and I want to make sure that when the application exits, the destructor will be executed. Is there a way I can define a static function that''s guranteed to be called when the application quits, so I can delete the instance from that function? Or is there another way? Any help is greatly appreciated.
I don''t know if there''s a way to guarantee the function is called, but have a static function called CMyClass::CleanUp or something that deletes m_pMyClass.
I''d suggest an auto_ptr, which is exactly what you could use, only it requires public access to your destructor (correct me if I''m wrong someone)
kill, since your singleton is instantiated on first use, you DO NOT need to use a pointer. You can simply do this :
class myClass{public:myClass* GetMyClass(){ static myClass obj; return &obj}//other functions};
This will give you 2 things :
1. Object is instantiated on first call of GetMyClass. 2. You are sure that the destructor will be called. It will be called somethime after main returned. You don''t know when, but It will.
I had exactly this same problem recently. And it''s interesting that the Design Patterns book doesn''t have anything to say on the problem of ensuring that Singletons get destroyed. My ''quick fix'' was to make a static Free() function, that in your case would simply be "delete m_pMyClass;" This will be safe even if you never created an instance of the singleton. Of course, you need to remember to put this in your program''s clean up code which, to me, seems to defeat the object a little. But, it works, and it''s safe.
Aha! Very cunning, Gorg. Yes, that works. You learn a new thing every day. Of course, it's not much good to me in my case because I need to ensure that the singleton is destroyed before the rest of the application. No big deal though.
Alternatively, you could use a static instance and return a reference to it: class CMyClass { public: ~CMyClass(); static CMyClass& GetMyClass(); private: CMyClass(); static CMyClass m_MyClass; }; CMyClass CMyClass::m_MyClass; CMyClass& CMyClass::GetMyClass() { return m_MyClass; }
A solution I read about on a website (I don''t recall which) created a static "janitor" inside the singleton that deleted the object in question in its destructor. Since the janitor is static it''s destructor will be called at program termination and thus it will delete the singleton''s object properly without explicit instructions from the programmer. Below is an example of a janitor assuming you have a proper base class to work with. If you don''t you can create a specific janitor or possibly use a void* and RTTI--I don''t know if the latter will work, I''ve never used RTTI.
class CMyClass { public: static CMyClass* GetMyClass();
private: CMyClass(); virtual ~CMyClass(); // MUST be virtual unless a specific janitor is used static CMyClass *m_pMyClass; static Janitor habib; // everyone''s favorite janitor };
So as you can see when the static section of CMyClass is being cleared up the destructor for habib will be called and hence the destructor for the singleton object will be called.