title: Access Violation: 0xC0000005, why is this happening?
body: <p>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.</p>
  
  <p>This code takes place inside an object called "Match" which has a full list of the players and corpses.</p>
  
  <p>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.</p>
  
  <p>When the player sends the use item message, this function is called:</p>
  
  <pre><code>void Player::use_res(){
      myMatch-&gt;res_corpse_under_player(this);
  }
  </code></pre>
  
  <p>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 <strong>Match.cpp</strong>:</p>
  
  <pre><code>bool intersect_inmatch(SFRECTANGLE a, SFRECTANGLE b){
    if (a.left &lt; b.right &amp;&amp; b.left &lt; a.right &amp;&amp; a.top &lt; b.bottom)
      return b.top &lt; 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-&gt;getPRECT();
      std::list&lt;Corpse&gt;::iterator cit;
      cit = corpses.begin();
      while(cit != corpses.end()){
  
          SFRECTANGLE crect;
          crect.left = cit-&gt;x;
          crect.top = cit-&gt;y;
          crect.right = cit-&gt;x + cit-&gt;mask.getSize().x;
          crect.bottom = cit-&gt;y + cit-&gt;mask.getSize().y;
  
          intersection = intersect_inmatch(prect, crect);
  
          if(intersection){
              return &amp;(*cit);
          }
  
          ++cit;
      }
      return NULL;
  }
  
  void Match::res_corpse_under_player(Player* player){
      cout &lt;&lt; "res corpse match function call" &lt;&lt; endl;
      Corpse* c = find_corpse_under_player(player);
      if(c != NULL){
          cout &lt;&lt; "found corpse" &lt;&lt; endl;
          cout &lt;&lt; "corpse name: " &lt;&lt; c-&gt;name &lt;&lt; endl;
          if(c-&gt;thisPlayer != NULL){
              cout &lt;&lt; "this player: " &lt;&lt; c-&gt;thisPlayer-&gt;name &lt;&lt; endl;
          }
      }
  }
  </code></pre>
  
  <p>I debugged it and the object appears to not be able to access any of the memory of itself after this line:</p>
  
  <blockquote>
    <p>intersection = intersect_inmatch(prect, crect);</p>
  </blockquote>
  
  <p>This function is where I try to see if the rectangles are overlapping.
  Here's a picture of the debug:
  <a href="http://i.imgur.com/M4yphMO.png" rel="nofollow">http://i.imgur.com/M4yphMO.png</a></p>
  
  <p>I tried stepping into the intersect_inmatch(...) call but for some reason it the debugger points back to this line:</p>
  
  <blockquote>
    <p>crect.bottom = cit->y + cit->mask.getSize().y;</p>
  </blockquote>
  
  <p>And then it points back to this line again:</p>
  
  <blockquote>
    <p>intersection = intersect_inmatch(prect, crect);</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>The exception happens at this line:</p>
  
  <blockquote>
    <p>cout &lt;&lt; "this player: " &lt;&lt; c->thisPlayer->name &lt;&lt; endl;</p>
    
    <p>Unhandled exception at 0x696C40F6 (msvcp110.dll) in Server.exe: 0xC0000005: Access violation reading location 0x00000000.</p>
  </blockquote>
  
  <p><strong>EDIT:</strong>
  Here is where I initially create the corpse object and push it to the list in my Match object:</p>
  
  <pre><code>//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-&gt;corpses.push_back(corpse);
  }
  </code></pre>