b2ContactListener::EndContact when object is destroyed

If a fixture gets destroyed, when it was touching another one, b2ContactListener::EndContact gets called. Also the manual states so.
Quote:
End Contact Event
This is called when two fixtures cease to overlap. This is called for sensors and non-sensors. This may be called when a body is destroyed, so this event can occur outside the time step.


On EndContact I need to access user data stored within the fixture, but cannot, since such function can be called even on fixtures which are being destroied (and that have invalid user data bound to them).

How am I supposed to know if one (or both) of the fixtures passed to b2ContactListener::EndContact is being destroyed?
Quote:
How am I supposed to know if one (or both) of the fixtures passed to b2ContactListener::EndContact is being destroyed?
Because you are the one who is destroying them.... aren't you? Box2D does not go around destroying things on it's own.

In any case, I have not checked but I very much doubt the user data is already invalid when you get the EndContact event - what would be the point of saying to the user, "hey, here is this invalid data for something which ended contact". Have you actually tried this yet?
I use world->IsLocked() to check if I was called during a step. I find this less ugly than managing a flag of my own.

But as irresistible force said, you just have to destroy the body before invalidating its userdata pointer, to be sure it will be valid when EndContact is called.
You are right guys, I should destroy my data only after the fixture has been destroyed.
This would be difficult to do without restructuring my API anyway, cause I destroy the fixture in the destructor of a class which my final object class inherits...

I will try checking world::IsLocked. Is it sure that such function is called when the world is not locked only if one of the two object is being destroyed?

Thanks btw.

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