From 4c7f56d4170e73ca1a3810b8d2a68613db8d26d8 Mon Sep 17 00:00:00 2001 From: "Addisu Z. Taddese" Date: Tue, 22 Aug 2023 14:41:57 -0500 Subject: [PATCH] Unregister collision detectors when the darstim plugin is unloaded (#529) Fixes a segfault that occurs due to destructors being removed from memory before they're called. --------- Signed-off-by: Addisu Z. Taddese --- dartsim/src/plugin.cc | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/dartsim/src/plugin.cc b/dartsim/src/plugin.cc index ed9029c15..28985ee1f 100644 --- a/dartsim/src/plugin.cc +++ b/dartsim/src/plugin.cc @@ -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) }