]> git.mxchange.org Git - flightgear.git/blobdiff - src/Input/FGJoystickInput.cxx
Fixed minor memory leak on joystick reload.
[flightgear.git] / src / Input / FGJoystickInput.cxx
index 75dc88700a1a2b93c6f4e4caf5b34dfa5d3c1434..afc7ad8fda42cafb6eaaa1a208082cb4f2885435 100644 (file)
@@ -27,6 +27,8 @@
 #include <Main/fg_props.hxx>
 #include <Scripting/NasalSys.hxx>
 
+using simgear::PropertyList;
+
 FGJoystickInput::axis::axis ()
   : last_value(9999999),
     tolerance(0.002),
@@ -53,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;
 }
 
 
@@ -66,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);
 
@@ -117,6 +135,13 @@ void FGJoystickInput::init()
   }
 }
 
+void FGJoystickInput::reinit() {
+  SG_LOG(SG_INPUT, SG_DEBUG, "Re-Initializing joystick bindings");
+  _remove();
+  FGJoystickInput::init();
+  FGJoystickInput::postinit();
+}
+
 void FGJoystickInput::postinit()
 {
   FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal");
@@ -166,7 +191,7 @@ void FGJoystickInput::postinit()
     string module = str.str();
     nasalsys->createModule(module.c_str(), module.c_str(), "", 0);
 
-    vector<SGPropertyNode_ptr> nasal = js_node->getChildren("nasal");
+    PropertyList nasal = js_node->getChildren("nasal");
     unsigned int j;
     for (j = 0; j < nasal.size(); j++) {
       nasal[j]->setStringValue("module", module.c_str());
@@ -176,7 +201,7 @@ void FGJoystickInput::postinit()
     //
     // Initialize the axes.
     //
-    vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
+    PropertyList axes = js_node->getChildren("axis");
     size_t nb_axes = axes.size();
     for (j = 0; j < nb_axes; j++ ) {
       const SGPropertyNode * axis_node = axes[j];
@@ -219,7 +244,7 @@ void FGJoystickInput::postinit()
     //
     // Initialize the buttons.
     //
-    vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
+    PropertyList buttons = js_node->getChildren("button");
     char buf[32];
     for (j = 0; j < buttons.size() && j < nbuttons; j++) {
       const SGPropertyNode * button_node = buttons[j];
@@ -234,7 +259,7 @@ void FGJoystickInput::postinit()
           continue;
       }
 
-      sprintf(buf, "%d", n_but);
+      sprintf(buf, "%u", (unsigned)n_but);
       SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
       bindings[i].buttons[n_but].init(button_node, buf, module );
 
@@ -265,6 +290,8 @@ void FGJoystickInput::update( double dt )
       continue;
 
     js->read(&buttons, axis_values);
+    if (js->notWorking()) // If js is disconnected
+      continue;
 
                                 // Fire bindings for the axes.
     for (int j = 0; j < bindings[i].naxes; j++) {