From 91ddbf6a7c2ada337251bbc4279ef3563538f228 Mon Sep 17 00:00:00 2001 From: James Hogan Date: Mon, 29 Aug 2022 21:54:09 +0100 Subject: [PATCH] VRManager: Fix crash on exit on Windows VRManager::instance() was using a function scoped static osg::ref_ptr to store the VRManager instance. However it needs to be used from fgOSCloseWindow(), which is called from an atexit handler, and C++11 specifies that static object destruction and atexit handlers are reverse sequenced, i.e. a static object initialised after an atexit call will be destroyed before the atexit callback is called. On Windows this can result in the osg::ref_ptr being destroyed (and hence set to NULL) before fgOSCloseWindow() tries to call destroyAndWait() on the instance. Fix the resulting seg fault by moving the ref_ptr object to static file scope so it is default constructed before the atexit call, and using only a simple static bool to initialise it on first call to VRManager::instance(). Reported-by: Alan Teeder --- src/Viewer/VRManager.cxx | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Viewer/VRManager.cxx b/src/Viewer/VRManager.cxx index fa768f51a..6fc921698 100644 --- a/src/Viewer/VRManager.cxx +++ b/src/Viewer/VRManager.cxx @@ -29,6 +29,11 @@ namespace flightgear { +// Unfortunately, this can't be scoped inside VRManager::instance(). +// If its initialisation completes after main() calls atexit(fgExitCleanup), +// then its destruction should take place before fgExitCleanup() is called. +static osg::ref_ptr managerInstance; + VRManager::VRManager() : _reloadCompositorCallback(new ReloadCompositorCallback(this)), _propXrLayersValidation("/sim/vr/openxr/layers/validation"), @@ -88,8 +93,12 @@ VRManager::VRManager() : VRManager *VRManager::instance() { - static osg::ref_ptr single = new VRManager; - return single; + static bool initialised = false; + if (!initialised) { + managerInstance = new VRManager; + initialised = true; + } + return managerInstance; } void VRManager::syncProperties()