My "this" Pointer Is Turning Null! ( C++ )


pvanukoff

Still Fresh
Joined
Apr 2, 2006
Messages
11
Age
48
Location
Scottsdale, AZ, USA
Website
www.amigaremix.com
It's been years since I've done C++ so I'm a bit rusty, however I can't recall ever seeing this problem before.

So I'm writing a game, and I have this problem, see relevent code below.

Relevant definitions:

Code:
// Game base class
class GameBase
{
	protected:

	// Fields
	Platform*   platform;   // platform we will poll inputs from and render outputs to
	StateLogic* stateLogic; // current state logic object

	// Constructor
	GameBase();

	public:

	// Destructor
	virtual ~GameBase();

	// Initialize
	virtual int Init( Platform* platform );

	// Start
	virtual int Start();

	// Do game processing (input, logic, output)
	virtual int Process( int timeDelta );

	// Clean up resources (after done)
	virtual int CleanUp();

	// Accessor for platform object
	Platform* GetPlatform();
};

// Running game states
enum GameState
{
	ShowTitle,	 // show title, start/options/exit, etc 
	ShowBestGames, // show top [X] best games 
	ShowDemo,	  // show a demo
	Playing,	   // currently playing
};

class TheGame : GameBase
{
	private:

	// Private fields
	GameState   curState;	  // current game state
	bool		applyNewState; // true if we need to apply the new state
	Level*	  curLevel;	  // current level
	Room*	   curRoom;	   // current room 
	Player*	 curPlayer;	 // current the player   
						
	public:

	// Constructor
	TheGame();
	
	// Destructor
	~TheGame();

	// // // Begin overridden methods

	// Initialize
	int Init( Platform* platform );

	// Start
	int Start();

	// Do game processing (input, logic, output)
	int Process( int timeDelta );

	// Clean up resources (after done)
	int CleanUp();

	// // // End

	// Set new game state
	int SetGameState( GameState newGameState, bool force );
	
	// Apply new game state
	int ApplyGameState();
};

Relevent implementation:

Code:
// Set current game state
int TheGame::SetGameState( GameState newGameState, bool force )
{
	TheGame* p = this;   // I have a breakpoint set here to see that the "this" pointer == null
	
	// Compare new to old state
	if( this->curState == newGameState )  // it blows up here because this == null
	{
		// State did not change -> do not apply new state
		applyNewState = false;
	}
	else
	{
		// State did change -> apply new state
		applyNewState = true;

		// Set new state
		this->curState = newGameState;
	}
	
	// Check for force
	if( force == true )
	{
		// Forcing new state to be applied
		applyNewState = true;
	}
	
	// Return
	return FN_OK;
}

So what is happening is this:

When my game starts, I create an instance of TheGame and call the Start() method, from my main game manager object, which in turn calls SetGameState() just to set the initial state of the game.

I have a breakpoint set at "TheGame* p = this;" so I can see what is happening. The first time the method is called, the "this" pointer is valid, and so "this->curState" is also valid.

Ok so all is well in the game until I need to change game state (going from title screen to the game for example). So, SetGameState() is called again, and again it hits the breakpoint, but this time around, the "this" pointer has become NULL. The code that calls the method the second time lives in another object, and looks like this:

Code:
// Tell game to switch to "ShowBestGames" state next time around
game->SetGameState( ShowBestGames, false );  // game is a reference to the instance of "TheGame"

How can this happen? I've placed a breakpoint on the above line and verified "game" is a valid pointer to "TheGame" class. If I can call the method, and the method is entered, how can the "this" pointer within that instance suddenly become NULL?

I'm using dev-c++ 4.9.9.2 and gcc version 3.4.2 (mingw-special).

Any help at all would be appreciated! :D
 
Thanks for looking guys.

I found the problem ... -> me <- ... it was my own dumb mistake. :wacko:

In the object that calls the method the second time, I was wrong, the "game" object is in fact null, and when I call game->SetGameState() it of course passes in a null "this" pointer.

Duh.

Thanks anyway.
 
Back
Top