Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Possibly for singleton class to delete its reference?

This topic is 5269 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Ive got a singleton class, I don''t like leaving my reference undeleted. So is the code at the bottom an ok way of deleteing it? And would i still bother deleting the class declarration eg. MyClass single; single = MyClass::getInstance(); delete single; Do i call the delete single? Is that right? i havent got the code with me but its something like this:
class MyClass {
        delete reference;
    static MyClass reference;
    MyClass getInstance()
        if (referene!=NULL)
            reference = new MyClass();

Share this post

Link to post
Share on other sites
my singleton works something like this

Class1 *C = Class1::getInstance();
C = Class1::destroyInstance(); // returns null
Class1::destroyInstance(); directly

one problem with your way would be if you dont have the class reference at hand the code would look strange (but still work):

delete MyClass::getInstance();

It looks bad because nowhere do you call new MyClass... (inside the singleton, but not in the code using the singleton)

Also, I use the atexit hack, to always make sure that all my singletons is deleted before the program exits...

Share this post

Link to post
Share on other sites

your method does not make sense. The destructor will only be called when delete is called on an instance, or when a statically allocated instance goes out of scope. In other words, your singleton will never be destroyed, and if somehow it were, it would cause an infinite recursion.

Deleting singletons is a tricky subject. The most straightforward approach may well be to have a static destroyInstance() method, as Nine said.

"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!