   Destruction order nightmare
  (Note: this thread was originally in Game Programming by accident)Luckily, I caught this bug before I even finished writing the code.To make this as clear as possible:I have 'graphics', 'sound', and 'input' singletons, all of which need to be created after an 'engine' singleton. That isn't a problem. However, they also need to be destroyed before it.Now, I am trying to use a singleton style that destroys the object when it goes out of scope (by having an auto_ptr type thing hold the pointer to the object). Are you still following me?I can't just check all three other singletons for destruction, in the destructor of 'engine' because if 'engine' is destroyed after one of the other three, but before another, the one that is already destroyed won't even exist anymore to be checked. How am I supposed to do this!?Any help is extremely appreciated.   
  Speaking in terms of broad generality, most methods of object creation in C++ use stack discipline in creation and destruction. So if engine is created before sound and input, then in most cases it would be deleted afterwards. Of course, without knowing how you plan to create them and store their references, it's hard to know what order they'd be destroyed in. Or if what you're concerned about will even be an issue.Since you haven't given much in the way of details, about the only concrete advice I can give is try finding a copy of "Modern C++ Design" by Andrei Alexandrescu. That book has a good discussion of singleton destruction.   ;  why not simply have your auto pointer like thingmajig nullify the pointer when it deletes it, and have the engine holder thingy just tell the other thingies to selfdestruct?   ;  I might be misunderstanding but the problem seems simple, assuming an instance of each class exists from the start to the finish of the application just use global pointers and new/delete in the order you want.If you want to say create the instances at the start of a function and then have them fall out of scope at the end (i think this might be what you want) i would say it was an unsafe way of doing it. Using new/delete even within the function would be better.;  Sorry, I should have given more details.I am writing an SDL wrapper.  SDL_Init must be called before any other function, and vice-versa for SDL_Quit.  Those two functions are in my Engine singleton (which the user doesn't even need to know about).  My other singletons, in their Get function, call Engine's Get (which I also hate... they reference a global).I could have simplied this scenario by saying that I need one object to destruct before another (because it is irrelavent that there are three).Someone suggested calling delete in order.  Well, I can't do that because the user of the class doesn't ever call delete; it is called when each object goes out of scope (by way of an auto_ptr equivilant).Someone else suggestion nullifying the pointer after deletion.  That would work, except once an object goes out of scope, it is gone forever (right??) so it can't be checked.  My problem is that they'll go out of scope randomly, and I won't be able to check if anything has been deleted, because I would be referencing something that isn't there.   ;  Don't use a Singleton?  Just place a graphics, sound, and input object in the engine, and let the engine object destroy them as it goes out of scope?  Seems simple enough.  Then you can have them destroyed in whatever order you desire.   ;Quote:Original post by David ONeilDon't use a Singleton?  Just place a graphics, sound, and input object in the engine, and let the engine object destroy them as it goes out of scope?  Seems simple enough.  Then you can have them destroyed in whatever order you desire.That's what I'll do, thanks for setting me on track! [smile]I had originally decided against it for a reason that is too complicated to explain, because it the reason is extremely false.Thanks a lot, man (and to all the others who tried to help before a gave enough information!).EDIT:  I'll go ahead and give the reason: I was trying to keep it seperate because I falsely thought that I would need to reference 'engine' from 'graphics', 'sound', and 'input', which I won't have to.  The other (valid) reason was that I simply wanted to keep them seperate, mainly for good looks.[Edited by - thedevdan on July 13, 2004 7:24:31 PM];  If you want to use singletons that depend on other singletons, there is a fairly simple solution. Create a class that maintains a stack. Singletons register with the class when they are instantiated. The last step of cleanup is to tell the class to delete all singletons on the stack. Here is a quick example off the top of my head: class Reaper{public:    class Client    {   virtual ~Client() = 0;    };    static Reaper * Instance()    {   if ( m_pInstance == 0 )   {  m_pInstance = new Reaper;   }   return m_pInstance;    }    static void Destroy()    {   delete m_pInstance;   m_pInstance = 0;    }    void Register( Reaper::Client * pClient )    {   m_Clients.push( pClient );    }private:    ~Reaper()    {   while ( !m_Clients.empty() )   {   delete m_Clients.top();   m_Clients.pop();   }    }    static Reaper * m_pInstance;    std::stack< Client * > m_Clients;};class SingletonExample : public Reaper::Client{    static SingletonExample * Instance()    {   if ( m_pInstance == 0 )   {  m_pInstance = new SingletonExample;   }   return m_pInstance;    } private:    SingletonExample()    {   // Initialize   ...   // Now register with the Reaper (must be last so all singletons this depends on are registered before this one)   Reaper::Instance()->Register( this );    }    ~SingletonExample()    {   assert( m_pInstance != 0 );   m_pInstance = 0;    }    static SingletonExample * m_pInstance;}; ;  Just move everything in your destructor into a function called Destroy() or something similar.  Then, just make sure to call Destroy() on your objects in the proper order.  You should also have an assert in your destructor to make sure the object was destroyed.  If it wasn't, then you forgot to put in the call to Destroy() somewhere.   ;Quote:Original post by JohnBoltonIf you want to use singletons that depend on other singletons, there is a fairly simple solution. Create a class that maintains a stack. Singletons register with the class when they are instantiated. The last step of cleanup is to tell the class to delete all singletons on the stack. Here is a quick example off the top of my head: *** Source Snippet Removed ***Very interesting... a singleton manager.  You saying this has given me tons of other ideas, too.  I will be choosing between this and the other idea of grouping them together (and of the ideas yours game me).  Thanks for your time, I'm now mad at myself for not thinking of that! [smile]   