Thread: Singleton: pointer not being destroyed.

  1. #1
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73

    Singleton: pointer not being destroyed.

    Hello there. I have been working on a Singleton class based off a sample code on a website which I can't remember (sorry for the author). I tried to test the class in a separate project and found that this little example works (i.e. displays twice the message) whereas I expect it to crash or at least output garbage on the 2nd time. Any idea why ?

    Code:
    //
    // system.h
    //
    
    #ifndef SINGLETON_H
    #define	SINGLETON_H
    
    #include <iostream>
    
    //****************************
    // Singleton class declaration
    //****************************
    
    template <typename T>
    class Singleton {
    private:
        static T* Instance;
    protected:
        Singleton<T> () { }
    public:
        static T* Create() {
            if(Instance == 0) {
                Instance = new T;
            }
            return Instance;
        }
    
        static void Destroy() {
            if(Instance != 0) {
                delete Instance;
                Instance = 0;
            }
        }
    };
    
    template <typename T>
    T* Singleton<T>::Instance = 0;
    
    class Child : public Singleton<Child> {
    public:
        void Whatever() { std::cout << "Inherited"; }
    };
    
    #endif	// SINGLETON_H
    Code:
    #include "singleton.h"
    
    int main(int argc, char** argv) {
        Child* instance = Singleton<Child>::Create();
        instance->Whatever();
        Child::Destroy();
        instance->Whatever();
    
        return 0;
    }
    I expect that after the call to Destroy() the memory will be deallocated and the pointer set to 0 but this does not seem to happen :/

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There is nowhere where the "Whatever" function is accessing member variables. Even though the 'this' pointer is NULL, it doesn't need to use that pointer to get at anything. In theory this is undefined behaviour, but in practice it tends to not crash.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Oh ok. Thank you. I thought that it might be undefined behaviour but wasn't so sure.

    Is your avatar an illustration of a sort in progress ?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yes it is actually. I used to change it from time to time, and for the moment it is on shellsort
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have I Destroyed Gravity?
    By bumfluff in forum Game Programming
    Replies: 25
    Last Post: 12-28-2006, 04:58 PM
  2. destroyed reference variable
    By Mehdi in forum C++ Programming
    Replies: 4
    Last Post: 08-14-2006, 12:54 AM
  3. How can Bit Torrent be destroyed?
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 07-31-2006, 01:53 PM
  4. Classes and Destroyed Instances
    By FWGaming in forum C++ Programming
    Replies: 3
    Last Post: 07-12-2005, 10:02 PM
  5. returning a pointer to a destroyed variable?
    By krygen in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2004, 02:22 PM