I'm currently developing my own C++ game using Box2D physics. However, when I try to quit the game, I encounter an access violation error with the following message:
"Exception error at 0x003A2AE4 in NinjaPlatformer.exe: 0xC0000005: Access violation while reading at position 0xFEEEFEF2"
The error seems to be occurring at the line b2Contact* c = ce->contact;
in the following code snippet:
for (b2ContactEdge* ce = body->GetContactList(); ce != nullptr; ce = ce->next) {
// Value of "ce" crash: 0xfeeefeee {other=??? contact=??? prev=??? ...}
b2Contact* c = ce->contact;
// Value of "c" at crash: 0x07932d78 {m_type=b2_dynamicBody | -17891604 (-17891602) m_flags=65262 m_islandIndex=-17891602 ...}
if (c->IsTouching()) {
// something
}
}
I guess that when attempting to close the application, the body contacts are deleted before the program loops back to the for loop, resulting in an error because the body is no longer defined. Is this correct? If so, how can I check within the for loop if ce
is not empty?
You must be care about edge conditions: the last valid b2ContactEdge* ce in your list has no initialized "next" member.
"next" has a invalid value 0xFEEEFEF2 at the last ce in your list.
I suggest you add this constructor a your b2ContactEdge class:
b2ContactEdge() { next = NULL; }