views:

220

answers:

4

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 writen in simple .c style, with no header, so yes I suppose the fucntion 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

A: 

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.

Adam Maras
+1  A: 

Your this pointer is null.

Something like this is happening:

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

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.

franmon
+1  A: 

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.

Naveen
Thansk for that, I fixed it by making each class store an actual object of itself instead of a pointer to an object. Not sure if that's what the link was telling me, ut it definitly helped me get to the conclusion. Although I still don't understand why this works, and the other way around it doesn't... if you could explain it I would be appreciated.
Zepee
This is my *guess*, I am not sure whether this is entirely the case:Since in both the constructors you are calling getInstance(), depending on the order of initialization of the cpp compilation units one of the pointers will be NULL (as the initialization has not yet happened). Which pointer becomes NULL is very hard to predict. Probably you can ask a different question on this, and the people who have more knowledge on this issue might be able to help.
Naveen