Destroying a contact listener

Hi,

I experimented a strange crash while working on my game. What I did was, when quitting, first destroy my contact listener, and then destroy all the bodies (using DestroyBody, and not during a step). This worked fine, except when I quit while a contact was existing: in those cases I got the following segfault:

Code:
#0  0x00007f16ab04ddbd in b2ContactManager::Destroy(b2Contact*) () from /usr/lib/libbox2d.so.0
#1  0x00007f16ab036dbb in b2PairManager::Commit() () from /usr/lib/libbox2d.so.0
#2  0x00007f16ab037eb1 in b2BroadPhase::DestroyProxy(int) () from /usr/lib/libbox2d.so.0
#3  0x00007f16ab035e6c in b2Shape::DestroyProxy(b2BroadPhase*) () from /usr/lib/libbox2d.so.0
#4  0x00007f16ab04e71b in b2World::DestroyBody(b2Body*) () from /usr/lib/libbox2d.so.0


I report this because I do not think this specific case is explicitly covered in the "implicit destruction" chapter of the manual.
Deleting the contact listener after the bodies solved my issue, but other users might want to change the current listener in mid-game, or simply do the same mistake as I.
Yes, please do not destroy your listeners until after the world is destroyed.
Could this be more enforced by the API ?
For example: having the SetContactListener method transfer ownership to the world. And then the world would be responsible for deleting things in the right order (and people would not be tempted to destroy the listener too early).
Gnurfos wrote:
Could this be more enforced by the API ?
For example: having the SetContactListener method transfer ownership to the world. And then the world would be responsible for deleting things in the right order (and people would not be tempted to destroy the listener too early).

Just speaking as a user, I don't know that this would be practical or desirable.

For one thing, the contact listener might not be an object that Box2D can reasonably take ownership of. For example, it might be an object on the stack, or it might be set to an object's own 'this' pointer, or it might point to an object that's managed by a smart pointer. Also, passing of ownership as you describe (without using a smart pointer to make the semantics explicit) would be confusing, IMO, and would have its own pitfalls. It would also make the API more complicated.

Attempting to protect the user from any and all user errors (such as deleting a contact listener prematurely) would, I imagine, make both the API and the internals of Box2D more complex and more difficult to maintain. Again though, this is just my take on it as a user of the library.
Yeah, I agree with Jesse.

However, you should be able to set the listener to NULL and then destroy your listener. However, the engine has a bug and cannot handle a NULL listener.
Jesse wrote:
the contact listener ... might be set to an object's own 'this' pointer


I just changed my code to something like that, so I can understand.
The current situation is not that bad, but it could be documented in the SetContactListener method's description.

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