Jump to content
  • Advertisement

Archived

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

bg3ntl

destroying singleton instances ?

This topic is 6003 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

well ok i understand singletons now and how to use them etc. when do they get destroyed ? do u create a destroy function and call that when u want to release the instance you have created ? or can u just put a call to delete m_singleton in the destructor. when the app quits will it automatically call the destructor for any remaining objects ? thanks for your help bg3ntl

Share this post


Link to post
Share on other sites
Advertisement
A lot depends on how you implemented your singleton. If you do it the ''usual'' way, using a static member pointer to a CSingleton, which you create using new the first time the GetInstance() function is called, the destructor never gets called unless you explicitly delete the singleton . For this reason, I prefer to use this method for my singletons - its cleaner and the destructor is called automagically when the program exits.

  
class CSingleton
{
private:
CSingleton()
{
// constructor stuff goes here

}
public
~CSingleton()
{
// Destructor stuff goes here

}
CSingleton* GetInstance()
{
static CSingleton instance;
return &instance;
}
};

Share this post


Link to post
Share on other sites
why is the destructor called in your case ?

yes you are right, i am doing it the usual way with a static class member m_singleton and a static accessor function.

is it because you are creating a static variable that has a lifetime equal to that of the program exectution ?

if that is true, how come a static member variable doesnt behave the same way ?

bg3ntl

Share this post


Link to post
Share on other sites
thnx kvh, that post was excellent. i never knew that static vars were destroyed in FILO manner. now i see the difference between the two implementations, one is created in free space and one is just a static var destroyed at the end of program execution.

thnx a lot!

bg3ntl

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!