Access Violation: 0xC0000005, why is this happening?

1.7k views Asked by At

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);
}
1

There are 1 answers

0
Joe Bid On

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.