1

Let's say I have 3 classes(A, B and C). Every class has an instance variable of the Singleton class and uses this instance variable in some functions of the class.

This class could look like this:

SingletonClass* mSingletonClass; // declared in header file

mSingletonClass = SingletonClass::getInstance(); // called in constructor

mSingletonClass->doSomething();     // called in function doSomething()
mSingletonClass->doSomething1();    // called in function doSomething1()

SingletonClass.h

class SingletonClass : public QObject {
    Q_OBJECT
public:
    static SingletonClass* getInstance();

    // some functions
private:
    SingletonClass();
    SingletonClass(SingletonClass const&);
    SingletonClass& operator=(SingletonClass const&);
    static SingletonClass* mInstance;
};

SingletonClass.m

SingletonClass* SingletonClass::mInstance = 0;

SingletonClass* SingletonClass::getInstance() {
    if (!mInstance) {
        mInstance = new SingletonClass();
    }

    return mInstance;
}

SingletonClass::SingletonClass() : QObject() {
}

How am I supposed to delete the instance variables?

If I call delete mSingletonClass; on every deconstructor(A, B and C), the application crashes.

Is it "enough" if I just call delete mSingletonClass; once, for instance in Class A?

Niklas
  • 23,674
  • 33
  • 131
  • 170
  • Duplicate of http://stackoverflow.com/questions/15733482/how-to-delete-singleton-object – hivert Mar 09 '14 at 11:37
  • This depends on how the `SingletonClass` is written. Do you have the source code for it? If the singleton is a static object, you don't need to delete it because it will be deleted when the application shuts down. – Ove Mar 09 '14 at 11:38
  • The anti pattern singleton is an instance which is available as one instance and for the rest of the lifetime of the program - just do not delete it. –  Mar 09 '14 at 11:38
  • @Ove I've included my SingletonClass. – Niklas Mar 09 '14 at 11:43

2 Answers2

6

You never destroy singleton object in any of the classes that use it. The very reason the singleton is a singleton is that there is only one instance of it in your system, created once, and kept forever, until your program is about to exit.

John Vlissides discusses one way to destroy singletons in his C++ Report article called To Kill A Singleton. He suggests using a special "guard" object that deletes a pointer to singleton upon destruction. In modern C++ you can achieve the same effect by pointing a unique_ptr<T> to the instance of your singleton (you can use it for your mInstance variable).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    You don't have to keep it forever though. E.g. there can be a factory that returns `shared_ptr`, the factory itself keeps a `week_ptr`. This way the singleton can be destroyed once the last reference to it is destroyed. – Maxim Egorushkin Mar 09 '14 at 13:40
  • @MaximYegorushkin That is true as well. The strategy of creating and releasing a singleton depends entirely on the way it is used - for example, on whether or not it needs to stay around [when nobody is around to reference it](http://en.wikipedia.org/wiki/If_a_tree_falls_in_a_forest). – Sergey Kalinichenko Mar 09 '14 at 13:52
2

You can put a static member variable (e.g. SingletonClassDestroyer) inside the SingletonClass to help destroy it since the lifetime of a static member variable ends when program terminates. SingletonClassDestroyer should take the SingletonClass instance and destroy it in its own destructor ~SingletonClassDestroyer(). Thus, SingletonClassDestroyer will help you automatically destroy SingletonClass instance in the end.

bagwell
  • 33
  • 5