Jump to content
  • Advertisement
Sign in to follow this  
Coluna

Problem with singletons-visual c++ 6.0

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

Hello; When i run my program in debug mode, everything goes OK, but when i set it to build the release, the program crashes. After a lot of bug tracking, i found that the singletons r being created a lot of times(!!!!!), and thus messing up the variables, seting pointers to null and so on...I think this occurs when optimizations settings is on (in project settings).Im using visual c++ 6.0, XP service pack2, visual studio service pack 6. Its not a singleton problem, but i know some of you will assume that, so this is the code im using:

private:
	cCOLUNA_GAME();
public:
	static cCOLUNA_GAME* Get_Instance()
	{
		static cCOLUNA_GAME coluna_game;
		return &coluna_game;		
	}

Did anyone had the same problem? WTF is that?

Share this post


Link to post
Share on other sites
Advertisement
I think the problem is that you are returning the address of the cCOLUNA_GAME variable on the stack, rather than a heap variable.

Perhaps try this:



static cCOLUNA_GAME::m_pInstance = new cCOLUNA_GAME();

class cCOLUNA_GAME()
{
private:
static cCOLUNA_GAME* m_pInstance;

public:
cCOLUNA_GAME();
~cCOLUNA_GAME();

static cCOLUNA_GAME* GetInstance() { return m_pInstance; }
};







I think that should work, but I'm more of a C# programmer.

I don't know whether it's important to you, but this isn't a thread-safe implementation of the Singleton pattern.

edit: various syntax errors

Share this post


Link to post
Share on other sites

class cCOLUNA_GAME()
{
private:
static cCOLUNA_GAME* m_pInstance;

public:
cCOLUNA_GAME();
~cCOLUNA_GAME();

//called to get instance
static cCOLUNA_GAME* GetInstance()
{
if (!m_pInstance)
{
m_pInstance = new cCOLUNA_GAME;
}
return m_pInstance;
}

//called at end of program so we don't leak any memeory
static void DeleteInstance()
{
delete m_pInstance;
m_pInstance = 0;
}
};

//cpp file
cCOLUNA_GAME *cCOLUNA_GAME::m_pInstance = 0;


Share this post


Link to post
Share on other sites
Quote:
private:
cCOLUNA_GAME();
public:
static cCOLUNA_GAME* Get_Instance()
{
static cCOLUNA_GAME coluna_game;
return &coluna_game;
}


cCOLUNA_GAME::Get_Instance is implicitly inline as it is declared within the class body. I'm guessing you're ending up with multiple instances of cCOLUNA_GAME::Get_Instance::coluna_game, either once per translation unit or once per use (not sure on the exact semantics of all this).

The simplest fix is to merely prototype Get_Instance within your class body:
static cCOLUNA_GAME* Get_Instance();

And then implement Get_Instance within a source file:
static cCOLUNA_GAME* cCOLUNA_GAME::Get_Instance()
{
static cCOLUNA_GAME coluna_game;
return &coluna_game;
}

Another alternative: Make coluna_game a class-member static:
private:
static cCOLUNA_GAME coluna_game;
public:
static cCOLUNA_GAME * Get_Instance() {
return & coluna_game;
}

In a source file:
cCOLUNA_GAME cCOLUNA_GAME::coluna_game;

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!