c++: Object's variable cannot be evaluated, but variable from reference to the same object can?

StackOverflow https://stackoverflow.com/questions/1817814

  •  10-07-2019
  •  | 
  •  

Question

Ok, this is veeery weird... I think. What I mean with the title is:

inside the act() function from an actionHandler object I have:

state->getHumanPieces();

Which gives me an address violation of some sort, apparently 'this' does not have a 'state' variable initialized... It so happens this actionHandler class has a static variable, which is a pointer to an instance of itself, called 'handler'... and if I do:

handler->state->getHumanPieces();

It works perfectly.. In order to make this even clearer:

That 'handler' pointer, points to the only instance of actionHandler existing in the whole program (singleton pattern).. So basically when I run this act() function from my actionHandler object, it doesn't let me access the 'state' variable, BUT if from that object, I try to access the same variable through a pointer to the same object, it is ok?? I don't get what is going on.. I'm not sure if it is clear, prob a bit confusing, but I hope it is understandable..

Btw, the VS08 debugger is showing what I mean:

this: 0x000000 {state=???}
   handler: someAddress {state= someAddress}
      handler:...
      state:...
state:  CXX0030: ERROR:  expression cannot be evaluated

I hope that makes it clearer, it's the little tree-structure that shows up on the little window where the current values of the variables are shown (Autos).

EDIT: I so get that the this pointer is null, I just don't understand how it can be null.. I'll post some code:

actionHandler.h:

class gameState;

class actionHandler
{ 
public:
        static actionHandler* Instance(){return handler;}
    void act(int,int);
private:
    actionHandler();
    static actionHandler* handler;
    gameState *state;
};

actionHandler.cpp:

actionHandler* actionHandler::handler = new actionHandler();

actionHandler::actionHandler()
{
        state = gameState::Instance();
}
void actionHandler::act(int x, int y)
{
    state->getHumanPieces();
}

now, in gameState.h i have a similar structure(singleton) and an actionHandler* private var, which gets initialised in:

gameState::gameState()
{
    handler = actionHandler::Instance();
}

and also a getHandler() func which returns the handler. This all should get initialised in main.cpp:

gameState *currState = gameState::Instance();
actionHandler *handler = currState->getHandler();

and then is used:

handler->act(event->button.x,event->button.y);

main.cpp is written in simple .c style, with no header, so yes I suppose the function calling the handler is static... however, I also make calls to the gameState* pointer, which supposedly works exactly in the same way as the actionHandler* one.. Hope this makes it more clear.

Was it helpful?

Solution

Looks like a case of static initialization order fiasco described here. Your both static objects constructors are depending upon each other in a circular fashion which is very odd.

OTHER TIPS

Your this pointer is null.

Something like this is happening:

actionHandler* actObj = 0;
actObj->act(); // ERROR access violation

Make sure the actionHandler object you're calling act() on is initialized. It looks to me like act() is getting called on a null pointer.

Are your actionHandler() or act() methods static?

Because if so, it is perfectly normal that your this pointer is NULL because static methods are not called from a particular instance of an object.

For instance, take an object that looks like this:

class CThing
{
public:
    static void actionHandler();
    void act();
protected:
    static CThing* handler;
    CState state;
}

If a function pointer on CThing::actionHandler() is passed to a third party class to get notified of something, when this class calls the CThing::actionHandler() method to notify you (by calling:CThing::actionHandler(); and not ptrThing->actionHandler() as it would if the actionHandler method was not static), you will not be able to access the this pointer nor the state variable from within actionHandler (or any subsequent calls made from within actionHandler) because there is no this pointer, there is no current object.

That is why when using the handler->state->getHumanPieces() it works, because now, you are actually referring to an instance of the CThing class (handler) and not the class definition itself.

I hope I was clear enough... If not, don't hesitate to ask for precisions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top