Skip to content

Commit

Permalink
Unregister collision detectors when the darstim plugin is unloaded (#529
Browse files Browse the repository at this point in the history
)

Fixes a segfault that occurs due to destructors being removed from memory before they're called.
---------

Signed-off-by: Addisu Z. Taddese <[email protected]>
  • Loading branch information
azeey authored and scpeters committed Oct 30, 2023
1 parent c4629f6 commit 4c7f56d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions dartsim/src/plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ class Plugin :
public virtual ShapeFeatures,
public virtual SimulationFeatures { };

namespace {

// This is done as a partial fix for
// https://github.com/gazebosim/gz-physics/issues/442. The issue seems like the
// destructors for the concrete collision detectors get unloaded and deleted
// from memory before the destructors run. When it's time to actually call the
// destructors, a segfault is generated.
//
// It's not clear why the destructors are deleted prematurely. It might be a
// compiler optimization in new compiler versions.
//
// The solution here is to call the `unregisterAllCreators` function from the
// plugins translation unit in the hopes that it will force the compiler to keep
// the destructors.
struct UnregisterCollisionDetectors
{
~UnregisterCollisionDetectors()
{
dart::collision::CollisionDetector::getFactory()->unregisterAllCreators();
}
};

UnregisterCollisionDetectors unregisterAtUnload;
}

IGN_PHYSICS_ADD_PLUGIN(Plugin, FeaturePolicy3d, DartsimFeatures)

}
Expand Down

0 comments on commit 4c7f56d

Please sign in to comment.