Sign in to follow this  

Singleton problem

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

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!

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

This topic is 4375 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this