EndContact not reporting when body is destroyed.

If two bodies are touching each other, resting on top of each other, and I destroy one of them, I don't get an EndContact() call. I think it might have something to do with the call getting stuck in Alchemy-land when b2ContactManager destroys a contact (?). Or I could be doing something wrong.

Thanks for any help.

- Doug
Are you destroying from any contact call? Because the world is locked at that point, and you shouldn't delete bodies.

-P
No, the destroy is done inside a key press listener. Looking deeper into both the C++ and ActionScript, it looks like the world's b2ContactManager class in C++ land has no way of being assigned a b2ContactListener from ActionScript land, so the call just doesn't get made in the first place, or gets made to a b2ContactListener only in C++ land. Could be way off though too 🙂
How are you destroying your body? Are you just removing it from the stage if you're using WCK? is "reportEndContact" definitely true? I'm 99% certain the problem is not something off in alchemy land.
I use the Alchemy port directly, so in this case I'm just calling b2World::DestroyBody(), which eventually calls lib.b2World_DestroyBody(m_world._ptr, _ptr). In the C++ version of Box2D, this invokes the world's b2ContactManager to make an EndContact() call to its m_contactListener class.

I think everything works fine in Alchemy land, but the call just isn't getting out to my b2ContactListener implementation in ActionScript.

All the report flags are set to true, so normal "EndContacts" get fired, i.e. if I pull the first body off the other body instead of destroying it.
Holy smokes, you're right! Here's the problem code (in Alchemy):

Code:
AS3_Val b2World_DestroyBody(void* data, AS3_Val args) {
   b2World* w;
   b2Body* b;
   AS3_ArrayValue(args, "PtrType, PtrType", &w, &b);
   AS3_Release((AS3_Val)b->m_userData);
   for(b2Fixture* f = b->m_fixtureList; f; f = f->m_next) {
      AS3_Release((AS3_Val)f->m_userData);
   }
   for(b2JointEdge* j = b->m_jointList; j; j = j->next) {
      AS3_Release((AS3_Val)j->joint->m_userData);
   }
   w->DestroyBody(b);
   return AS3_Null();
}



See how it's calling AS3_Release on all the fixture userData before calling DestroyBody? Well that means once the end contact event is dispatched, the userData (which points to the AS3 instance) is no longer around to dispatch an event from. Working on a fix...
Ok, the latest commit to github should fix this problem. Turns out I didn't even need to release everything there, because I was already doing it in the destruction listener. Of course, alchemy happily marched along as if nothing was wrong.

Sorry I doubted you! I was thinking that if this bug were true, it would wreak havoc on all my other projects.
Yea it's actually odd that I haven't ran into this before either. Your fix does the trick for me though.

As usual, you're very quick to address issues. I appreciate it.
Hi, I think I've run into the same problem and I'm just wondering if I've got the latest build. I'm downloading the .ZIP from here https://github.com/jesses/wck. Is there a separate, more up to date github page for the alchemy port without the rest of the wck?

I'm trying to resize a box at runtime which involves destroying and recreating the fixture on the body. b2ContactListener EndContact doesn't seem to be being called when I destroy the fixture. I'll carry on looking just to make certain the problem isn't in my code.
I solved it by saving all contacts in an associated object and manually removing them when the fixture is destroyed. Not great but it works! I tried changing the fixture without destroying it but box2d didn't seem to only kindof like that. It works but not very well.

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