Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

Stack overflow

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

My sdl wrapper is a singleton class, confusing if more than one class handles the drawing. Anyways it looks like this: SdlWrapper.h
class SdlWrapper {
	// SINGLETON ---

protected:
	static SdlWrapper* m_pInstance;
	SdlWrapper( void ) { SetRes(640, 480, 32); };	// set default res and init the data

public:
	static SdlWrapper* GetInstance( void ) {
		if ( !m_pInstance )
			m_pInstance = new SdlWrapper();
		return m_pInstance;
	}

	static void DeleteInstance(void) {
		delete m_pInstance;
		m_pInstance = 0;
	}

	// --- ---

   ...

And in main I run:
int main (int argc, char *argv[]) {	
	SdlWrapper* Sdl = Sdl->GetInstance();
        delete Sdl;
        return 1;
}

But then I get Stack Overflow at the line:
	static void DeleteInstance(void) {
->		delete m_pInstance;
		m_pInstance = 0;
	}
What have I done wrong? It worked before I put that delete sdl there, shouldnt I have it? I found myself never doing SDL_FreeSurface on images or deleting pointers. Shouldnt I do that kind of things? Or shouldnt singleton pointers be deleted?

Share this post


Link to post
Share on other sites
Advertisement
Is your destructor calling DeleteInstance()? If so, then you have an infinite loop: destructor calls DeleteInstance() which calls your destructor which calls DeleteInstance()...

Share this post


Link to post
Share on other sites
Depending on the singleton implementation, from what you've posted, usually the destructors are private as well as the constructor, and you would create/delete the singleton through your static GetInstance and DeleteInstance. Following that, you wouldn't be able to call delete Sdl in your main app, and would need to do SdlWrapper::DeleteInstance();

Share this post


Link to post
Share on other sites
Yes my deconstructor calls delete instance:


SdlWrapper::~SdlWrapper() {
SDL_FreeSurface(this->screen);
this->DeleteInstance();
}

[/code

So what should I do? Call the delete instance from main?

Sdl->DeleteInstance();

Or remove the deleteinstance call from the destrcutor and just delete the pointer?

delete sdl;

Share this post


Link to post
Share on other sites
delete Sdl calls ~SdlWrapper,
which calls DeleteInstance,
which does delete m_pInstance,
which calls ~SdlWrapper,
which calls DeleteInstance,
which does delete m_pInstance,
which calls ~SdlWrapper,
which calls DeleteInstance,
which does delete m_pInstance,
...

Take DeleteInstance out of the destructor, make ~SdlWrapper protected, and use DeleteInstance in main.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!