memory leak fixed 49/20449/4
Cedric Delamarre [Wed, 8 Aug 2018 08:07:50 +0000 (10:07 +0200)]
Change-Id: Ia691470ed70a02f6011ec279a06334817444a06c

scilab/modules/ast/includes/ast/serializervisitor.hxx
scilab/modules/ast/src/cpp/ast/visitor_common.cpp
scilab/modules/io/sci_gateway/cpp/sci_genlib.cpp
scilab/modules/matio/matio.vcxproj
scilab/modules/matio/matio.vcxproj.filters
scilab/modules/matio/sci_gateway/c/sci_matfile_varwrite.c
scilab/modules/matio/src/c/DllmainMatio.c [deleted file]
scilab/modules/matio/src/c/matfile_manager.c

index 017b838..eeff5e5 100644 (file)
@@ -608,6 +608,10 @@ private :
 
 public :
     SerializeVisitor(Exp* _ast) : ast(_ast), buf(NULL), buflen(0), bufsize(0), saveNodeNumber(true), saveLocation(true) {}
+    ~SerializeVisitor()
+    {
+        if(buf) free(buf);
+    }
 
     SerializeVisitor* clone()
     {
index c1e73db..319f225 100644 (file)
@@ -12,7 +12,7 @@
  * along with this program.
  *
  */
+
 #include <string>
 #include <numeric>
 #include <iostream>
@@ -2383,7 +2383,6 @@ ast::Exp* callTyper(ast::Exp* _tree, std::wstring _msg)
         timer.check(msgD.c_str());
     }
 
-    free(newast);
     delete s;
     delete d;
     return newTree;
index 2285824..af0bab9 100644 (file)
@@ -346,7 +346,6 @@ types::Function::ReturnValue sci_genlib(types::typed_list &in, int _iRetCount, t
             }
 
             delete s;
-            free(serialAst);
             delete parser.getTree();
         }
 
index c7a24b0..10b202e 100644 (file)
@@ -214,7 +214,6 @@ lib /DEF:"$(ProjectDir)Sparse_f_Import.def" /SUBSYSTEM:WINDOWS /MACHINE:$(Platfo
     <ClCompile Include="src\c\CreateIntegerVariable.c" />
     <ClCompile Include="src\c\CreateMatlabVariable.c" />
     <ClCompile Include="src\c\CreateSparseVariable.c" />
-    <ClCompile Include="src\c\DllmainMatio.c" />
     <ClCompile Include="src\c\GetMatlabVariable.c" />
     <ClCompile Include="src\c\matfile_manager.c" />
   </ItemGroup>
index b763a1c..8080367 100644 (file)
@@ -42,9 +42,6 @@
     <ClCompile Include="src\c\CreateSparseVariable.c">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="src\c\DllmainMatio.c">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="src\c\GetMatlabVariable.c">
       <Filter>Source Files</Filter>
     </ClCompile>
index 015713e..9ee729d 100644 (file)
@@ -162,7 +162,7 @@ int sci_matfile_varwrite(char *fname, void* pvApiCtx)
     }
 
     flag = Mat_VarWrite(matfile, matvar, compressionFlag);
-
+    Mat_VarFree(matvar);
     /* Return execution flag */
     var_type = (flag == 0);
     createScalarBoolean(pvApiCtx, Rhs + 1, var_type);
diff --git a/scilab/modules/matio/src/c/DllmainMatio.c b/scilab/modules/matio/src/c/DllmainMatio.c
deleted file mode 100644 (file)
index 3e9c6d8..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
- * Copyright (C) 2006 - INRIA - Allan CORNET
- *
- * Copyright (C) 2012 - 2016 - Scilab Enterprises
- *
- * This file is hereby licensed under the terms of the GNU GPL v2.0,
- * pursuant to article 5.3.4 of the CeCILL v.2.1.
- * This file was originally licensed under the terms of the CeCILL v2.1,
- * and continues to be available under such terms.
- * For more information, see the COPYING file which you should have received
- * along with this program.
- *
- */
-/*--------------------------------------------------------------------------*/
-#include <windows.h>
-/*--------------------------------------------------------------------------*/
-//for Visual Leak Detector in debug compilation mode
-//#define DEBUG_VLD
-#if defined(DEBUG_VLD) && defined(_DEBUG)
-#include <vld.h>
-#endif
-/*--------------------------------------------------------------------------*/
-#pragma comment(lib,"../../bin/libintl.lib")
-#pragma comment(lib,"../../bin/libmatio.lib")
-/*--------------------------------------------------------------------------*/
-int WINAPI DllMain (HINSTANCE hInstance , DWORD reason, PVOID pvReserved)
-{
-    switch (reason)
-    {
-        case DLL_PROCESS_ATTACH:
-            break;
-        case DLL_PROCESS_DETACH:
-            break;
-        case DLL_THREAD_ATTACH:
-            break;
-        case DLL_THREAD_DETACH:
-            break;
-    }
-    return 1;
-}
-/*--------------------------------------------------------------------------*/
-
index 2c2c04b..0d4c629 100644 (file)
@@ -111,3 +111,67 @@ void matfile_manager(int action, int *fileIndex, mat_t **matfile)
         /* Return NULL */
     }
 }
+
+static void clearOpenedMatfiles()
+{
+    if(openedMatfiles)
+    {
+        int i = 0;
+        for(i = 0; i < numberOfMatfiles; ++i)
+        {
+            if(openedMatfiles[i])
+            {
+                Mat_Close(openedMatfiles[i]);
+            }
+        }
+        FREE(openedMatfiles);
+        numberOfMatfiles = 0;
+    }
+}
+
+/*** free memory at library unload ***/
+#ifdef _MSC_VER
+#include <windows.h>
+/*--------------------------------------------------------------------------*/
+//for Visual Leak Detector in debug compilation mode
+//#define DEBUG_VLD
+#if defined(DEBUG_VLD) && defined(_DEBUG)
+#include <vld.h>
+#endif
+/*--------------------------------------------------------------------------*/
+#pragma comment(lib,"../../bin/libintl.lib")
+#pragma comment(lib,"../../bin/libmatio.lib")
+/*--------------------------------------------------------------------------*/
+int WINAPI DllMain(HINSTANCE hinstDLL, DWORD flag, LPVOID reserved)
+{
+    switch (flag)
+    {
+        case DLL_PROCESS_ATTACH :
+            break;
+        case DLL_PROCESS_DETACH :
+            clearOpenedMatfiles();
+            break;
+        case DLL_THREAD_ATTACH :
+            break;
+        case DLL_THREAD_DETACH :
+            break;
+        default :
+            return 0;
+    }
+
+    return 1;
+}
+#else
+void __attribute__ ((constructor)) loadmatio(void);
+void __attribute__ ((destructor)) unloadmatio(void);
+
+void loadmatio(void)
+{
+
+}
+
+void unloadmatio(void)
+{
+   clearOpenedMatfiles();
+}
+#endif
\ No newline at end of file