1

Currently working on my game server. This exception happens when a player sends a message to use a particular item when he is over a corpse object's rectangle.

This code takes place inside an object called "Match" which has a full list of the players and corpses.

One minute my object is fine, I can read all of its variables values, no garbage values. Then all of a sudden, I can't read any of the memory in the object I'm in. For no reason what so ever. This eventually leads to an access violation exception.

When the player sends the use item message, this function is called:

void Player::use_res(){
    myMatch->res_corpse_under_player(this);
}

I give the player I want to check if it's over a corpse to this function in the Match object. So now we're in the match object. Here are the three functions that take place for this event which are located in Match.cpp:

bool intersect_inmatch(SFRECTANGLE a, SFRECTANGLE b){
  if (a.left < b.right && b.left < a.right && a.top < b.bottom)
    return b.top < a.bottom;
  else
    return false;
}

//Find the corpse that's undernearth this player
//corpse loop exception fix attempt
Corpse* Match::find_corpse_under_player(Player* player){
    bool intersection = false;
    SFRECTANGLE prect = player->getPRECT();
    std::list<Corpse>::iterator cit;
    cit = corpses.begin();
    while(cit != corpses.end()){

        SFRECTANGLE crect;
        crect.left = cit->x;
        crect.top = cit->y;
        crect.right = cit->x + cit->mask.getSize().x;
        crect.bottom = cit->y + cit->mask.getSize().y;

        intersection = intersect_inmatch(prect, crect);

        if(intersection){
            return &(*cit);
        }

        ++cit;
    }
    return NULL;
}

void Match::res_corpse_under_player(Player* player){
    cout << "res corpse match function call" << endl;
    Corpse* c = find_corpse_under_player(player);
    if(c != NULL){
        cout << "found corpse" << endl;
        cout << "corpse name: " << c->name << endl;
        if(c->thisPlayer != NULL){
            cout << "this player: " << c->thisPlayer->name << endl;
        }
    }
}

I debugged it and the object appears to not be able to access any of the memory of itself after this line:

intersection = intersect_inmatch(prect, crect);

This function is where I try to see if the rectangles are overlapping. Here's a picture of the debug: https://i.stack.imgur.com/J2KpA.png

I tried stepping into the intersect_inmatch(...) call but for some reason it the debugger points back to this line:

crect.bottom = cit->y + cit->mask.getSize().y;

And then it points back to this line again:

intersection = intersect_inmatch(prect, crect);

I try stepping into it again but now it goes over it. After that, the object appears to not be able to read any of its memory (step 3 in picture). I have no idea why this happens. What could possibly be doing this? I've been up for 6 hours trying to figure out why but I can't find out why.

The exception happens at this line:

cout << "this player: " << c->thisPlayer->name << endl;

Unhandled exception at 0x696C40F6 (msvcp110.dll) in Server.exe: 0xC0000005: Access violation reading location 0x00000000.

EDIT: Here is where I initially create the corpse object and push it to the list in my Match object:

//Player.cpp
Player::make_corpse_out_of_this_player(){
    Corpse corpse(x, y, this); //this == instance of Player, setting Player* thisPlayer pointer to this in Corpse object.
    corpse.name = string(name);
    corpse.mask = mask;
    myMatchPointer->corpses.push_back(corpse);
}
Joe Bid
  • 465
  • 8
  • 24
  • Maybe `c->thisPlayer->name` is `NULL`? Also, `intersect_inmatch` should just be `return a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom;`. (Separating the tests that way makes no sense.) – David Schwartz Jun 10 '15 at 11:19
  • Looks like c->thisPlayer->name is NULL? – fassl Jun 10 '15 at 11:21
  • 1
    It looks like you're trying to debug an optimized build. Turn that off to see what's happening in `intersect_inmatch`. – tsuki Jun 10 '15 at 11:26
  • Are you absolutely sure that `find_corpse_under_player` returns a pointer to a valid object? It may have become invalid long before this point. (Using pointers to list elements is an open invitation to undefined behaviour unless you're *very* careful.) – molbdnilo Jun 10 '15 at 11:29
  • David Schwartz, when I output c->thisPlayer->name, it's completely blank. But the thing is if you look in the picture, the entire Match ("this") object cannot read any of it's own memory. Why? Why wouldn't it be able to read itself?? tsuki, I'm not sure what you mean? Using Visual Studio 2012 Express. How do I turn this off? molbdnilo, yes it is returning a valid object. However a pointer in the Corpse object at this point in the code, is not null, but I cannot display the name: http://pastebin.com/xcYc6Eqn And yes I set the name of it. It's only when I try to access it and cout it. – Joe Bid Jun 10 '15 at 18:24
  • c->thisPlayer is not null as you can see from the pastebin. But for some reason it's name is invalidated. I have no idea why. Updated my code with where I initially create the corpse object and push it to the list at the bottom. – Joe Bid Jun 10 '15 at 18:36
  • The debugger showing you the wrong line of code is generally caused when you're debugging one version of the binary with a different version of source. Are you sure you got the code compiled and deployed? And you're not running a cached version of the binary? – Jay Jun 10 '15 at 21:15

1 Answers1

0

Turns out I wasn't actually setting c->thisPlayer when I first create my Corpse object and push it to the list so it had a garbage value.

Joe Bid
  • 465
  • 8
  • 24