Jump to content
  • Advertisement
Sign in to follow this  
JasonHise

Strange behavior with static instance regarding singleton

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

The following was my attempt to build a reliable singleton base which would support singletons that depended upon each other. The public inst member should be moved to private, but it is publicly available right now because I am debugging it. Can anyone explain to me why everything works fine when I uncomment the first line in main, but when I leave it as is, the static instance never gets created? It would seem that the compiler is making an optimization that it shouldn't be allowed to make, and I have all optimizations turned off anyhow.
#include <iostream>

template < typename T >
class Singleton
{
private:
	static int dependents;
	static T * self;

	Singleton (  )
	{
		self = 0;
		dependents = 0;
		AddDependent (  );
	}

	~ Singleton (  )
	{
		RemoveDependent (  );
	}

public:
	static Singleton < T > inst;

	static void AddDependent (  )
	{
		if ( !dependents++ )
		{
			try
			{
				self = new T (  );
			}
			catch ( ... )
			{
				--dependents;
				throw;
			}
		}
	}

	static void RemoveDependent (  )
	{
		if ( !--dependents )
		{
			delete self;
		}
	}

	static T * GetInst (  )
	{
		return self;
	}
};

template < typename T >
T * Singleton < T > :: self;

template < typename T >
int Singleton < T > :: dependents;

template < typename T >
Singleton < T > Singleton < T > :: inst;

// SAMPLE SINGLETON CLASSES

class Debugger
{
private:
	friend class Singleton < Debugger >;

	int error;

	Debugger (  )
	{
		error = 42;
	}

	~ Debugger (  )
	{
	}

public:
	int GetError (  )
	{
		return error;
	}
};

/*class DXGfx
{
private:
	friend class Singleton < DXGfx >;

	DXGfx (  )
	{
		Singleton < Debugger > :: AddDependent (  );
	}

	~ DXGfx (  )
	{
		Singleton < Debugger > :: RemoveDependent (  );
	}

public:
	int GetGfxError (  )
	{
		return Singleton < Debugger > :: GetInst (  )->GetError (  );
	}
};*/

// DEMO PROGRAM

int main (  )
{
	//Singleton < Debugger > & debugger = Singleton < Debugger > :: inst;
	std::cout << Singleton < Debugger > :: inst.GetInst (  )->GetError (  ) << std::endl;
	return 0;
}


Share this post


Link to post
Share on other sites
Advertisement
Your problem is that you are trying to call a static member through an instance. However, when the compiler sees that, it thinks that you really mean Singleton < Debugger > :: GetInst ( ), and don't really need to access the variable at all, so the compiler doesn't bother to construct the static object. Try changing GetInst() from a static member function to a non-static member function, and it should work.

Share this post


Link to post
Share on other sites


#include <iostream>

template < typename T >
class Singleton
{
private:
static int dependents;
static T * self;
static Singleton < T > inst;

Singleton ( )
{
self = 0;
dependents = 0;
AddDependent ( );
}

~ Singleton ( )
{
RemoveDependent ( );
}

public:
static void AddDependent ( )
{
// NOT SURE WHY I NEEDED THIS, BUT
// WITHOUT IT INST IS NEVER CREATED
Singleton < T > & instcheck = inst;

if ( !dependents++ )
{
try
{
self = new T ( );
}
catch ( ... )
{
--dependents;
throw;
}
}
}

static void RemoveDependent ( )
{
if ( !--dependents )
{
delete self;
}
}

static T * GetInst ( )
{
return self;
}
};

template < typename T >
T * Singleton < T > :: self;

template < typename T >
int Singleton < T > :: dependents;

template < typename T >
Singleton < T > Singleton < T > :: inst;

// SAMPLE SINGLETONS

class Debugger
{
private:
friend class Singleton < Debugger >;

int error;

Debugger ( )
{
error = 42;
}

Debugger ( const Debugger & rhs );

~ Debugger ( )
{
}

public:
int GetError ( )
{
return error;
}
};

class DXGfx
{
private:
friend class Singleton < DXGfx >;

DXGfx ( )
{
Singleton < Debugger > :: AddDependent ( );
}

DXGfx ( const DXGfx & rhs );

~ DXGfx ( )
{
Singleton < Debugger > :: RemoveDependent ( );
}

public:
int GetGfxError ( )
{
return Singleton < Debugger > :: GetInst ( )->GetError ( );
}
};

// DEMO PROGRAM

int main ( )
{
std::cout << Singleton < DXGfx > :: GetInst ( )->GetGfxError ( ) << std::endl;
return 0;
}



This is really bizzare. I got it working, but at the expense of a fix I don't really understand. I have a big comment by it. Why does the compiler only recognize that inst needs to be created when I create an additional referance to it?

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!