Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should btBroadphaseInterface destructor remove itself from btCollisionObject broadphase proxies? #567

Closed
radarsat1 opened this issue Feb 19, 2016 · 2 comments

Comments

@radarsat1
Copy link
Contributor

I don't know Bullet very well so apologies if this doesn't make sense. But, I just fixed a crash-on-exit bug in a program that uses Bullet. The problem turned out to be that a object managing Bullet was using std::shared_ptrs to keep btBroadphaseInterface and btCollisionWorld pointers, among other things. I noticed that the problem was fixed if I explicitly destroyed the btCollisionWorld object instead of letting the shared_ptrs take care of it.

It turns out that during the btCollisionWorld destructor, it makes some dereferences of a broadphase proxy that references the btBroadphaseInterface. However, due to the order of the variables declared in the manager class, the btBroadphaseInterface was getting destroyed first, leading to a segfault during the btCollisionWorld destructor.

I was wondering if an acceptable solution would be for btBroadphaseInterface to remove any proxies that point to it during its destructor? That way there would be no order dependency between their destructors. However, maybe it's impossible, or even wasteful, since it would require a sweep through all broadphase objects just before they are destroyed. Otherwise, any ideas on how such a segfault could be warned against or avoided?

@erwincoumans
Copy link
Member

Yes, it is recommended to explicitly destroy objects in the reverse order of construction, don't use smart pointers etc. Memory in Bullet is managed explicitly in the right order, not in random arbitrary order.
I envy C-API's that make such smart pointer mess-up harder :)

@radarsat1
Copy link
Contributor Author

Ok!

azeey added a commit to gazebosim/gz-physics that referenced this issue Apr 7, 2023
This fixes two tests with segfaults on macOS

* COMMON_TEST_simulation_features_bullet-featherstone: The failure appears to be due to the destruction order of the triangleMeshes and meshesGImpact containers. Per [this] (Should btBroadphaseInterface destructor remove itself from btCollisionObject broadphase proxies?  bulletphysics/bullet3#567 (comment)) comment, memory in bullet is sensitive to order of destruction. meshesGImpact objects contain pointers to items contained bytriangleMeshes, so the solution is to ensure meshesGImpact is destructed before triangleMeshes.
* INTEGRATION_DoublePendulum: This is a DART test, but the segfault seems to be related to Bullet. I found that even after removing all the code in DARTDoublePendulum.cc, the segfault still occurs as long as it's linked with libdart-collision-bullet. Since the test doesn't need the bullet collision detector, I've opted to just remove the library from the DART_LIBRARIES variable. I still don't know the root cause though, so this might affect the actual dartsim-plugin library.

Signed-off-by: Addisu Z. Taddese <addisu@openrobotics.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants