<Unable to read memory> error

I keep getting this error and I can't figure out why. This error is saying this error for member variable mpCurrentState and mInitialStateID. These variables are part of class StateMachine. I set them in the StateMachine constructor to NULL for mpCurrentState and -1 for minitialStateID, yet by the time the update function is hit they have nothing. I cout in the constructor and they are initialized, but something happens to make them unreadable and I can't figure out what.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

//state machine class
class StateMachine :public Trackable
{
public:
	StateMachine();
	~StateMachine(){};

	void addState(StateMachineState* pState);
	void setInitialStateID(const SM_idType& id){ std::cout << "state set\n";  mInitialStateID = id; };

	void update();//give the current state a chance to run
	void start();//go to the initial state

protected:
	void transitionToState(const SM_idType& targetID);//call onExit for old state and onEntrance for the new state

	std::map<SM_idType, StateMachineState*> mStates;//all states indexed by stateID
	StateMachineState* mpCurrentState;//the current state that is running
	SM_idType mInitialStateID;//the id of the state which should run first
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

//constructor
StateMachine::StateMachine() :mpCurrentState(NULL), mInitialStateID(-1)
{
	std::cout << "current state: " << mpCurrentState << std::endl;
}

//update function
void StateMachine::update()
{
        //breaks on this if statement!!!!!!!!!
	if (mpCurrentState == NULL)
	{
		start();
		assert(mpCurrentState != NULL);
	}

	StateTransition* pTransition = mpCurrentState->update();
	if (pTransition != NULL)
	{
		transitionToState(pTransition->getTargetStateID());
	}
}
Last edited on
Hi,

Try using nullptr instead of NULL. NULL is 0, and nullptr was invented to avoid this type of confusion.

Other than that, consider using a debugger - always instructive for complex code that compiles.
Last edited on
Why do you want to print a pointer with the value NULL ?????

1
2
3
4
5
//constructor
StateMachine::StateMachine() :mpCurrentState(NULL), mInitialStateID(-1)
{
	std::cout << "current state: " << mpCurrentState << std::endl;
}


@TheIdeasMan

Try using nullptr instead of NULL.


nullptr works only in C++11 - so maybe not with his compiler.
Last edited on
I print the pointer with a null value to make sure it is a null value and not garbage. Any who, a friend found my problem. A silly mistake. I was put a type before the state machine's variable when I was initializing it so it wasn't using the state machine member variable in the class that was using it. whoops.

1
2
3
4

//this was the mistake
StateMachine* mpStateMachine = new StateMachine();


1
2
3
4

//this is what it should have been
mpStateMachine = new StateMachine();
Last edited on
I print the pointer with a null value to make sure it is a null value and not garbage.


A better way would be
1
2
3
#include <assert.h>
assert(mpCurrentState == NULL);
(
Topic archived. No new replies allowed.