Fix memory leaks found by coverity (CID 1098781 & 1098880)
[scilab.git] / scilab / modules / external_objects_java / src / cpp / ScilabJavaEnvironment.cpp
index 93b666e..fea094e 100644 (file)
@@ -23,6 +23,7 @@
 #include "ScilabJavaObject.hxx"
 #include "ScilabJavaArray.hxx"
 #include "ScilabJavaCompiler.hxx"
+#include "ScilabOperations.hxx"
 #include "NoMoreScilabMemoryException.hxx"
 #include "ScilabAutoCleaner.hxx"
 
@@ -65,7 +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());
     }
 
     return envId;
@@ -77,9 +77,9 @@ void ScilabJavaEnvironment::finish()
     {
         ScilabEnvironments::unregisterScilabEnvironment(envId);
         envId = -1;
+        instance->Finalize();
         delete instance;
         instance = 0;
-        instance->Finalize();
         usable = false;
     }
 }
@@ -233,9 +233,24 @@ int ScilabJavaEnvironment::newinstance(int id, int * args, int argsSize)
 
 int ScilabJavaEnvironment::operation(int idA, int idB, const OperatorsType type)
 {
-    // TODO: plug String concatenation and maybe others things like operations on double, int, ...
+    JavaVM *vm = getScilabJavaVM();
+    int ret;
 
-    return 0;
+    switch (type)
+    {
+        case Add :
+            ret = ScilabOperations::add(vm, idA, idB);
+            break;
+        default :
+            throw ScilabJavaException(__LINE__, __FILE__, gettext("Invalid operation"));
+    }
+
+    if (ret != 0 && ret != -1)
+    {
+        ScilabAutoCleaner::registerVariable(envId, ret);
+    }
+
+    return ret;
 }
 
 int * ScilabJavaEnvironment::invoke(int id, const char * methodName, int * args, int argsSize)
@@ -248,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;
 }
@@ -335,6 +353,20 @@ void ScilabJavaEnvironment::removeobject(int id)
     ScilabAutoCleaner::unregisterVariable(envId, id);
 }
 
+void ScilabJavaEnvironment::removeobject(const int * id, const int length)
+{
+    if (length == 1)
+    {
+        removeobject(*id);
+    }
+    else
+    {
+        JavaVM *vm = getScilabJavaVM();
+        ScilabJavaObject::removeScilabJavaObject(vm, id, length);
+        ScilabAutoCleaner::unregisterVariable(envId, id, length);
+    }
+}
+
 void ScilabJavaEnvironment::autoremoveobject(int id)
 {
     JavaVM *vm = getScilabJavaVM();
@@ -364,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;
 }