From 99c809dda061b8dcb1efb9b140c07dfbd6a09d43 Mon Sep 17 00:00:00 2001 From: Florent Rougon Date: Sun, 14 Jan 2018 00:00:28 +0100 Subject: [PATCH] FGMouseInput: add shutdown() and reinit() overrides - Modify global_mouseInput in init() and in shutdown(), because *these* are the places where FGMouseInput is enabled or disabled. - reinit() does shutdown() followed by init(). Note: the commented-out block starting with "FIXME: memory leak" that is removed here was just an outdated comment, because SGBindingList is an std::vector, where SGBinding_ptr is a smart pointer type (SGSharedPtr). In other words, there was no leak in this place---at least, not recently. --- src/Input/FGMouseInput.cxx | 46 +++++++++++++++++--------------------- src/Input/FGMouseInput.hxx | 6 +++-- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/Input/FGMouseInput.cxx b/src/Input/FGMouseInput.cxx index 62bf07203..68ab15b78 100644 --- a/src/Input/FGMouseInput.cxx +++ b/src/Input/FGMouseInput.cxx @@ -111,7 +111,7 @@ void ActivePickCallbacks::update( double dt, unsigned int keyModState ) */ struct mouse_mode { mouse_mode (); - virtual ~mouse_mode (); + FGMouseCursor::Cursor cursor; bool constrained; bool pass_through; @@ -347,7 +347,7 @@ public: // The Mouse Input Implementation //////////////////////////////////////////////////////////////////////// -static FGMouseInput* global_mouseInput = NULL; +static FGMouseInput* global_mouseInput = nullptr; static void mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea) { @@ -361,21 +361,12 @@ static void mouseMotionHandler(int x, int y, const osgGA::GUIEventAdapter* ea) global_mouseInput->doMouseMotion(x, y, ea); } - - -FGMouseInput::FGMouseInput() -{ - global_mouseInput = this; -} - -FGMouseInput::~FGMouseInput() -{ - global_mouseInput = NULL; -} +FGMouseInput::FGMouseInput() = default; void FGMouseInput::init() { SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings"); + d.reset(new FGMouseInputPrivate()); std::string module = ""; @@ -457,6 +448,23 @@ void FGMouseInput::init() fgRegisterMouseClickHandler(mouseClickHandler); fgRegisterMouseMotionHandler(mouseMotionHandler); + global_mouseInput = this; +} + +void FGMouseInput::shutdown() +{ + SG_LOG(SG_INPUT, SG_DEBUG, "Shutting down mouse bindings"); + + // This ensures that mouseClickHandler and mouseMotionHandler are no-ops. + global_mouseInput = nullptr; + // Reset the Pimpl + d.reset(); +} + +void FGMouseInput::reinit() +{ + shutdown(); + init(); } void FGMouseInput::update ( double dt ) @@ -522,18 +530,6 @@ mouse_mode::mouse_mode () { } -mouse_mode::~mouse_mode () -{ - // FIXME: memory leak -// for (int i = 0; i < KEYMOD_MAX; i++) { -// int j; -// for (j = 0; i < x_bindings[i].size(); j++) -// delete bindings[i][j]; -// for (j = 0; j < y_bindings[i].size(); j++) -// delete bindings[i][j]; -// } -} - void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea) { if (!d) { diff --git a/src/Input/FGMouseInput.hxx b/src/Input/FGMouseInput.hxx index 838d9aec4..acf1c2ffa 100644 --- a/src/Input/FGMouseInput.hxx +++ b/src/Input/FGMouseInput.hxx @@ -40,10 +40,12 @@ namespace osgGA { class GUIEventAdapter; } //////////////////////////////////////////////////////////////////////// class FGMouseInput : public SGSubsystem, FGCommonInput { public: - FGMouseInput(); - virtual ~FGMouseInput(); + FGMouseInput(); + virtual ~FGMouseInput() = default; void init() override; + void shutdown() override; + void reinit() override; void update( double dt ) override; static const char* subsystemName() { return "input-mouse"; }