Fix memory leaks found by coverity (CID 1098781 & 1098880)
[scilab.git] / scilab / modules / external_objects_java / src / cpp / ScilabJavaEnvironment.cpp
index 2ec4063..fea094e 100644 (file)
@@ -66,8 +66,6 @@ int ScilabJavaEnvironment::start()
         instance->Initialize();
         instance->helper.setUseLastName(true);
         instance->helper.setNewAllowed(true);
-        //instance->enabletrace((std::string(getTMPDIR()) + std::string("/eo_java.log")).c_str());
-        //instance->enabletrace(std::string("/tmp/eo_java.log").c_str());
     }
 
     return envId;
@@ -79,9 +77,9 @@ void ScilabJavaEnvironment::finish()
     {
         ScilabEnvironments::unregisterScilabEnvironment(envId);
         envId = -1;
+        instance->Finalize();
         delete instance;
         instance = 0;
-        instance->Finalize();
         usable = false;
     }
 }
@@ -265,7 +263,10 @@ int * ScilabJavaEnvironment::invoke(int id, const char * methodName, int * args,
     invokedId[0] = 1 ; //1 object returned
     invokedId[1] = ScilabJavaObject::invoke(vm, id, methodName, args, argsSize);
 
-    ScilabAutoCleaner::registerVariable(envId, invokedId[1]);
+    if (invokedId[1] != 0 && invokedId[1] != -1)
+    {
+        ScilabAutoCleaner::registerVariable(envId, invokedId[1]);
+    }
 
     return invokedId;
 }
@@ -395,7 +396,9 @@ std::vector<std::string> ScilabJavaEnvironment::getCompletion(int id, char ** fi
     for (int i = 0; i < len; i++)
     {
         v.push_back(fields[i]);
+        delete fields[i];
     }
+    delete fields;
 
     return v;
 }