From 1456635c558aa95c898d01f5c7ad50a773f6ff58 Mon Sep 17 00:00:00 2001 From: ThorstenB Date: Wed, 13 Apr 2011 21:30:11 +0200 Subject: [PATCH] Fixed minor memory leak on joystick reload. --- src/Input/FGJoystickInput.cxx | 25 ++++++++++++++++++++----- src/Input/FGJoystickInput.hxx | 2 ++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Input/FGJoystickInput.cxx b/src/Input/FGJoystickInput.cxx index cf7cc3319..afc7ad8fd 100644 --- a/src/Input/FGJoystickInput.cxx +++ b/src/Input/FGJoystickInput.cxx @@ -55,10 +55,15 @@ FGJoystickInput::joystick::joystick () FGJoystickInput::joystick::~joystick () { -// delete js? why not? -// delete js; + // delete js? no, since js == this - and we're in the destructor already. delete[] axes; delete[] buttons; + jsnum = 0; + js = NULL; + naxes = 0; + nbuttons = 0; + axes = NULL; + buttons = NULL; } @@ -68,13 +73,24 @@ FGJoystickInput::FGJoystickInput() FGJoystickInput::~FGJoystickInput() { + _remove(); } +void FGJoystickInput::_remove() +{ + SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true); + js_nodes->removeChildren("js", false); + for (int i = 0; i < MAX_JOYSTICKS; i++) + { + if (bindings[i].js) + delete bindings[i].js; + bindings[i].js = NULL; + } +} void FGJoystickInput::init() { jsInit(); - // TODO: zero the old bindings first. SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings"); SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true); @@ -121,8 +137,7 @@ void FGJoystickInput::init() void FGJoystickInput::reinit() { SG_LOG(SG_INPUT, SG_DEBUG, "Re-Initializing joystick bindings"); - SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true); - js_nodes->removeChildren("js", false); + _remove(); FGJoystickInput::init(); FGJoystickInput::postinit(); } diff --git a/src/Input/FGJoystickInput.hxx b/src/Input/FGJoystickInput.hxx index 039401e29..392fc54b3 100644 --- a/src/Input/FGJoystickInput.hxx +++ b/src/Input/FGJoystickInput.hxx @@ -52,6 +52,8 @@ public: static const int MAX_JOYSTICK_BUTTONS = 32; private: + void _remove(); + /** * Settings for a single joystick axis. */ -- 2.39.5