Crash when destroying body after collision

Hi Erin,

first thanks for putting this effort into Box2D. It helped me to develope a prototype for my open source game in two weeks which otherwise would have taken months...

Now to the issue:
I'm experiencing a crash with SVN Rev. 81 if I destroy a b2body after a collision occured (using b2World::Destroy()).
Everything goes fine if I destroy the body immediately when detecting the collision (using b2World::GetContactList()). But if I delay the destruction until after the next b2World::Step(), the program crashes in b2ContactManager::Destroy() trying to access a bad body2 pointer.

You can reproduce the crash with the HelloWorld example. Please see the attached SVN patch for the changes that need to be made to helloworld.cpp.

Regards
I get the same problem with revision 84, with b2ContactManager::Collide() finding a retired contact with one of its shapes pointing to deleted memory (paved with 0xFDFDFDFD) and crashing inside b2ContactManager::Destroy(). I put a conditional breakpoint into b2World::Destroy for the shape that (later) crashes, and traced into b2Shape::DestroyProxy, which calls b2BroadPhase::DestroyProxy, which calls b2PairManager::Commit, which ultimately calls b2ContactManager::PairRemoved, but the pairUserData is m_nullContact so the contact gets skipped.

This is a case where reference counting would help, as the body would not expire until all references released it. The existing boost::intrusive_ptr class makes reference counting easy and efficient, though it's not hard to create a ref-counting smart pointer (intrusive or not). I definitely recommend reference counting instead of raw deletion, as it can eliminate the machinations required to avoid accessing deleted objects. The main downside is reference-count leaks preventing objects from being deleted, but smart pointers can helps a lot with that. Just beware of circular references!
Thanks, I already fixed this in my local build.

I was trying to defer contact destruction so that all results would be available after the time step. But I had already gotten rid of deferred body destruction.

Deferred destruction is no longer used because there will be multiple collision events that come and go in a single time step due to continuous physics processing. So the only way to get reliable contact data will be to use callbacks.
Ah, I see.

My game project has been using SVN revision 69 successfully for some time now, but branched off a build incorporating changes required by the 2.0 WIP around revision 80. I was concerned that I was using it wrong somehow, but now I know that's just how revision 84 is. 😀
I'm looking forward to see the fix in SVN 🙂
Hi,

Did a fix for this make it into 1.4.3? I didn't see it mentioned in the changelog, and it seems to be occurring here.

Cheers
George
This is fixed in the SVN version (2.0). 2.0 will be out in a few days.

My practice is not to change existing versions, but to release new versions. A bug fix would change the last digit. So if I had applied just the fix to version 1.4.3, it would have changed to version 1.4.4.
Hi,

i'm still experiencing this bug on box2d 2.0.1 release when a circle hits square.
i thought 0xfdfdfdfd was an additional useless point so i ignored it and now it came out that i dont get that valid circle-square collision point.
Only circles that act as bullets are messing collision points.

I downloaded newest SVN repo but its totally different and merging it would take way to much time for me just to check if it works, so i'm asking is there other way out?

Still it's weird that i got this error when it was fixed. 'o_0
Apoptyzm,

Please explain in detail the problem you are having.
Well i don't know how to help, im just playing with box2d.

Circles works fine when i put them loose, but i get dead pointer when a circle is in my bullet class which just holds pointers, info and is in Shape->UserData.
It doesn't matter if its a circle or square and what size/density/etc it is.

I don't know where to put breakpoints in code before pointer is set to 0xfd... or XCC0017 (visual studio error code for body)
so the code goes like:
world->step();
there are no broken pointers in Collision Listener added,
then when im trying to copy pointers from Contact Listener right after world->step() i get only shapes with broken pointers(0xfdfdfdfd in UserData);

I don't think this helps... i'm not going to learn box sourcecode to search for a bug either but i still would like to fix it :F ...

Original thread: https://box2d.org/forum/viewtopic.php?t=298