Singleton problem

Started by
4 comments, last by jpetrie 18 years, 3 months ago
Hey everyone! Im currently writing an input manager for my game using DirectInput. I decided to use a singleton as there should only be one instance. This is causing me problems. Heres my class:

class cInput
{
   static cInput*       m_pInstance;
   IDirectInput8*       m_pDirectInput;
//etc.
   cInput ();
   ~cInput ();

public:
   static cInput* GetInstance () 
   {
      return m_pInstance; 
   }
   bool Initialize ();

};

My question is: How do I access/use m_pDirectInput? That is, I am getting a memory access violation with this code..

// I initialize the class like this:
if (! cInput::GetInstance()->Initialize () )
{
}

// The initialize method..
bool cInput::Initialize ()
{
   // dont recreate DirectInput!
   if (m_pDirectInput) // *** MEMORY ACCESS VIOLATION ***
      return true;

  // init DirectInput
}


I cant seem to find out what is wrong. m_pDirectInput is nulled out in the constructor. m_pInstance is also allocated in the constructor. Everything worked okay before I added the singleton code. Also, Although I null out m_pDirectInput in the constructor, my debugger doesnt say that. I believe this is the source of my problem--but I cant find a solution! Im sure its probably something simple Im missing.. Please help! Any help is appreciated! Thanks in advance!
Advertisement
I do a few things differently.

1. I make the pointer to the instance, default constructor, copy constructor, and assignment operator private.

2. I make the destructor public

3. At the top of the .cpp you need to initialize the static pointer like this

cInput *cInput::m_pInstance = 0;


4. I have a GetInstance and DeleteInstance functions. Here are mine.

////////////////////////////////////////////////////////////////////	Function: GetInstance////	Purpose: Creates and instance if none exists and returns the //				instance if it already does//////////////////////////////////////////////////////////////////CGameApplication* CGameApplication::GetInstance(void){	// If the instance is not created then	// create one	if (!m_pInstance)		m_pInstance = new CGameApplication;	// Return the instance	return m_pInstance;}////////////////////////////////////////////////////////////////////	Function: DeleteInstance////	Purpose: Destroys the instance//////////////////////////////////////////////////////////////////void CGameApplication::DeleteInstance(void){	// If we have a valid instance delete it	if (m_pInstance)	{		delete m_pInstance;		m_pInstance = NULL;	}}


Hope that helps :)
have you actually allocated the memory for the singleton? As otherwise you're trying to call a method on nothing, its probably causing an exception on accessing the singleton as a whole rather than that specifed member
Personally, I really think people over-uses singletons, but using boost you can create a singleton pretty simple without the need for a "DestroyInstance" function:

[source lang = "cpp"]typedef boost::shared_ptr < class MySingleton > MySingletonPtr;class MySingleton{public: static MySingletonPtr GetInstance();private: // alternatively static MySingletonPtr m_pInstance;}MySingletonPtr MySingleton::m_pInstance;MySingletonPtr MySingleton::GetInstance(){ static MySingletonPtr pSingleton( new MySingleton ) return pSingleton; // alternatively if( m_pInstance == 0 )  m_pInstance.reset( new MySingleton ); return m_pInstance;}


There should ne no problem to implement this class as a baseclass.
Ethereal
I found it! Thanks everyone!

I made a silly mistake--I had the constructor allocate
the singleton ptr. Because the constructor could never
be called-the ptr was never allocated! Oops [smile]

I dont use singletons too often--only for the major systems.
The input manager needs to be a singleton so other classes
(like my window wrapper) could access it.

Ill take a look at std::boost.

Thanks alot! [smile]
To be pedantic, it's not std::boost. Just boost. Start here.

I'm not touching the singleton topic at all....

This topic is closed to new replies.

Advertisement