fix memory leak @ scilab startup 64/14764/8
Antoine ELIAS [Fri, 27 Jun 2014 16:27:37 +0000 (18:27 +0200)]
Change-Id: I33e57affca48d38c0dc055a34deebd84edfad570

36 files changed:
scilab/modules/api_scilab/src/cpp/api_common.cpp
scilab/modules/ast/includes/deserializervisitor.hxx
scilab/modules/ast/includes/exps/arraylistexp.hxx
scilab/modules/ast/includes/exps/arraylistvar.hxx
scilab/modules/ast/includes/exps/boolexp.hxx
scilab/modules/ast/includes/exps/callexp.hxx
scilab/modules/ast/includes/exps/doubleexp.hxx
scilab/modules/ast/includes/exps/functiondec.hxx
scilab/modules/ast/includes/exps/matrixlineexp.hxx
scilab/modules/ast/includes/exps/selectexp.hxx
scilab/modules/ast/includes/exps/seqexp.hxx
scilab/modules/ast/includes/exps/stringexp.hxx
scilab/modules/ast/src/cpp/run_OpExp.cpp
scilab/modules/ast/tests/unit_tests/matrix_exp.tst
scilab/modules/core/sci_gateway/cpp/sci_getscilabmode.cpp
scilab/modules/core/sci_gateway/cpp/sci_getversion.cpp
scilab/modules/core/src/cpp/InitScilab.cpp
scilab/modules/fileio/sci_gateway/cpp/sci_pathsep.cpp
scilab/modules/fileio/src/c/fileinfo.c
scilab/modules/functions_manager/src/cpp/dynamic_modules.cpp
scilab/modules/history_manager/src/cpp/HistoryFile.cpp
scilab/modules/io/sci_gateway/c/sci_getenv.c
scilab/modules/io/src/c/getenvc.c
scilab/modules/io/src/cpp/loadlib.cpp
scilab/modules/jvm/src/c/loadOnUseClassPath.c
scilab/modules/localization/src/c/setgetlanguage.c
scilab/modules/startup/src/cpp/scilab.cpp
scilab/modules/string/sci_gateway/cpp/sci_strsubst.cpp
scilab/modules/string/sci_gateway/cpp/sci_tokens.cpp
scilab/modules/string/src/c/strsubst.c
scilab/modules/symbol/includes/libraries.hxx
scilab/modules/symbol/includes/variables.hxx
scilab/modules/types/src/cpp/function.cpp
scilab/modules/types/src/cpp/macro.cpp
scilab/modules/types/src/cpp/macrofile.cpp
scilab/modules/types/src/cpp/string.cpp

index a6b6527..71dba4b 100644 (file)
@@ -125,7 +125,7 @@ int* assignOutputVariable(void* _pvCtx, int _iVal)
 
 int returnArguments(void* _pvCtx)
 {
-    return 0;
+    return 1;
 }
 
 int checkInputArgument(void* _pvCtx, int _iMin, int _iMax)
index 9f4442a..06e8947 100644 (file)
@@ -219,7 +219,9 @@ private :
     symbol::Symbol* get_Symbol(void)
     {
         std::wstring* s = get_wstring();
-        return new symbol::Symbol(*s);
+        symbol::Symbol *sym = new symbol::Symbol(*s);
+        delete s;
+        return sym;
     }
 
     double get_double(void)
@@ -265,12 +267,14 @@ private :
             {
                 std::wstring* s = get_wstring();
                 exp = new StringExp(*loc, *s);
+                delete s;
                 break;
             }
             case 3:
             {
                 std::wstring* s = get_wstring();
                 exp = new CommentExp(*loc, s);
+                //delete s;
                 break;
             }
             case 6:
@@ -597,4 +601,4 @@ public :
     }
 };
 }
-#endif /* !__DESERIALIZER_HXX__  */
\ No newline at end of file
+#endif /* !__DESERIALIZER_HXX__  */
index eb00a7f..966079b 100644 (file)
@@ -44,14 +44,22 @@ public:
 
     virtual ~ArrayListExp ()
     {
-        delete _exps;
+        std::list<Exp *>::const_iterator it = _exps->begin();
+        std::list<Exp *>::const_iterator itEnd = _exps->begin();
+        for (; it != itEnd ; ++it)
+        {
+            delete *it;
+        }
+
+       delete _exps;
     }
 
     virtual ArrayListExp* clone()
     {
         std::list<Exp *>* exps = new std::list<Exp *>;
-        std::list<Exp *>::const_iterator it;
-        for (it = _exps->begin() ; it != _exps->end() ; it++)
+        std::list<Exp *>::const_iterator it = _exps->begin();
+        std::list<Exp *>::const_iterator itEnd = _exps->begin();
+        for (; it != itEnd ; ++it)
         {
             exps->push_back((*it)->clone());
         }
index 5a44b55..c8307de 100644 (file)
@@ -44,16 +44,24 @@ public:
 
     virtual ~ArrayListVar ()
     {
+        std::list<Var *>::iterator it = _vars->begin();
+        std::list<Var *>::iterator itEnd = _vars->end();
+        for (; it != itEnd ; ++it)
+        {
+            delete *it;
+        }
+
         delete _vars;
     }
 
     virtual ArrayListVar* clone()
     {
         std::list<Var *>* vars = new std::list<Var *>;
-        std::list<Var *>::const_iterator it;
-        for (it = vars_get().begin() ; it != vars_get().end() ; it++)
+        std::list<Var *>::const_iterator it = _vars->begin();
+        std::list<Var *>::const_iterator itEnd = _vars->end();
+        for (;it != itEnd ; ++it)
         {
-            Var* var = dynamic_cast<Var*>((*it)->clone());
+            Var* var = static_cast<Var*>((*it)->clone());
             vars->push_back(var);
         }
 
index 4c9103a..b1cdb4e 100644 (file)
@@ -39,10 +39,7 @@ public:
         if (_bigBool)
         {
             _bigBool->DecreaseRef();
-            if (_bigBool->isDeletable())
-            {
-                delete _bigBool;
-            }
+            _bigBool->killMe();
         }
     }
     /** \} */
@@ -89,14 +86,12 @@ public:
     /** \brief Set the big bool */
     void setBigBool(types::Bool *pB)
     {
-        if (_bigBool && _bigBool->isRef())
+        if (_bigBool)
         {
             _bigBool->DecreaseRef();
-            if (_bigBool->isDeletable())
-            {
-                delete _bigBool;
-            }
+            _bigBool->killMe();
         }
+        
         _bigBool = pB;
         _bigBool->IncreaseRef();
     }
index d1c621c..a3a00a6 100644 (file)
@@ -44,13 +44,15 @@ public:
 
     virtual ~CallExp ()
     {
-        delete _name;
-        std::list<Exp *>::const_iterator i;
-        for (i = _args->begin() ; i != _args->end() ; i++)
+        std::list<Exp *>::const_iterator it = _args->begin();
+        std::list<Exp *>::const_iterator itEnd = _args->end();
+        for (;it != itEnd ; ++it)
         {
-            delete *i;
+            delete *it;
         }
+
         delete _args;
+        delete _name;
     }
 
     virtual CallExp* clone()
index 5954b8b..b3d911c 100644 (file)
@@ -41,10 +41,7 @@ public:
         if (_bigDouble)
         {
             _bigDouble->DecreaseRef();
-            if (_bigDouble->isDeletable())
-            {
-                delete _bigDouble;
-            }
+            _bigDouble->killMe();
         }
     }
     /** \} */
@@ -89,14 +86,12 @@ public:
 
     void setBigDouble(types::Double *pdbl)
     {
-        if (_bigDouble && _bigDouble->isRef())
+        if (_bigDouble)
         {
             _bigDouble->DecreaseRef();
-            if (_bigDouble->isDeletable())
-            {
-                delete _bigDouble;
-            }
+            _bigDouble->killMe();
         }
+        
         _bigDouble = pdbl;
         _bigDouble->IncreaseRef();
     }
index 18cfa82..8fa9c1e 100644 (file)
@@ -62,7 +62,8 @@ public:
 
     virtual ~FunctionDec ()
     {
-        delete _body;
+        //body will be deleted by types::Macro
+        //delete _body;
         delete _args;
         delete _returns;
         delete &_name;
index 94506eb..5e0d0fa 100644 (file)
@@ -44,11 +44,13 @@ public:
 
     virtual ~MatrixLineExp ()
     {
-        std::list<Exp *>::const_iterator i;
-        for (i = _columns->begin() ; i != _columns->end() ; i++)
+        std::list<Exp *>::const_iterator it = _columns->begin();
+        std::list<Exp *>::const_iterator itEnd = _columns->end();
+        for (; it != itEnd ; ++it)
         {
-            delete *i;
+            delete *it;
         }
+        
         delete _columns;
     }
 
index 11972f8..93ffa74 100644 (file)
@@ -52,7 +52,16 @@ public :
     ~SelectExp()
     {
         delete _selectme;
+        
+        cases_t::iterator it = _cases->begin();
+        cases_t::iterator itEnd = _cases->end();
+        for(; it != itEnd ; ++it)
+        {
+            delete *it;
+        }
+        
         delete _cases;
+        
         if (_default != NULL)
         {
             delete _default;
index 5d98be6..ceea686 100644 (file)
@@ -48,12 +48,6 @@ public:
         std::list<Exp *>::const_iterator i;
         for (i = _l_body->begin() ; i != _l_body->end() ; i++)
         {
-            FunctionDec* pDec = dynamic_cast<FunctionDec*>(*i);
-            if (pDec)
-            {
-                //do not delete function declaration.
-                continue;
-            }
             delete *i;
         }
         delete _l_body;
@@ -101,6 +95,11 @@ public:
     {
         return *_l_body;
     }
+
+    void exps_clear()
+    {
+        _l_body->clear();
+    }
     /** \} */
 
 
index 0df384a..d40ebf5 100644 (file)
@@ -39,10 +39,7 @@ public:
         if (_bigString)
         {
             _bigString->DecreaseRef();
-            if (_bigString->isDeletable())
-            {
-                delete _bigString;
-            }
+            _bigString->killMe();            
         }
     }
     /** \} */
@@ -77,13 +74,10 @@ public:
 
     void setBigString(types::String *_pS)
     {
-        if (_bigString && _bigString->isRef())
+        if (_bigString)
         {
             _bigString->DecreaseRef();
-            if (_bigString->isDeletable())
-            {
-                delete _bigString;
-            }
+            _bigString->killMe();
         }
         _bigString = _pS;
         _bigString->IncreaseRef();
index dae7720..809f9a1 100644 (file)
@@ -236,6 +236,7 @@ void RunVisitorT<T>::visitprivate(const LogicalOpExp &e)
             if (pIL->isComputable())
             {
                 pITL = pIL->extractFullMatrix();
+                pIL->killMe();
             }
         }
 
@@ -272,6 +273,7 @@ void RunVisitorT<T>::visitprivate(const LogicalOpExp &e)
                     if (pIR->isComputable())
                     {
                         pITR = pIR->extractFullMatrix();
+                        pIR->killMe();
                     }
                 }
                 pResult = GenericLogicalAnd(pITL, pITR);
@@ -325,14 +327,10 @@ void RunVisitorT<T>::visitprivate(const LogicalOpExp &e)
         result_set(pResult);
 
         //clear left and/or right operands
-        if (pITL->isDeletable())
+        pITL->killMe();
+        if(pITR)
         {
-            delete pITL;
-        }
-
-        if (pITR && pITR->isDeletable())
-        {
-            delete pITR;
+            pITR->killMe();
         }
     }
     catch (ast::ScilabError error)
index fee5054..78d1a27 100644 (file)
@@ -13,7 +13,7 @@
 a=[[] []; [] []];
 a=[1 2];
 a=[1;2];
-a=[1 2;]
+a=[1 2;];
 a=[1 2;3 4];
 a=[1 2:4;5:7 3];
 a=[1 2:4 [];[] [] 5:7 [] 3];
index 23ab327..a1428c9 100644 (file)
@@ -20,6 +20,7 @@ extern "C"
 #include "Scierror.h"
 #include "localization.h"
 #include "os_strdup.h"
+#include "sci_malloc.h"
 }
 /*--------------------------------------------------------------------------*/
 
@@ -36,7 +37,8 @@ Function::ReturnValue sci_getscilabmode(types::typed_list &in, int _iRetCount, t
         return Function::Error;
     }
 
-    String* pS = new String(getScilabModeString());
+    const char* pst = getScilabModeString();
+    String* pS = new String(pst);
     out.push_back(pS);
 
     return Function::OK;
index 9323eec..911732d 100644 (file)
@@ -25,6 +25,7 @@ extern "C"
 #include "localization.h"
 #include "Scierror.h"
 #include "with_module.h"
+#include "freeArrayOfString.h"
 }
 /*--------------------------------------------------------------------------*/
 #define VERSION_STRING L"string_info"
@@ -64,7 +65,7 @@ Function::ReturnValue sci_getversion(types::typed_list &in, int _iRetCount, type
             String* pOut2 = new String(1, iOption);
             pOut2->set(pwstOption);
             out.push_back(pOut2);
-            FREE(pwstOption);
+            freeArrayOfWideString(pwstOption, iOption);
         }
 
     }
index f21a11c..d12e62b 100644 (file)
@@ -424,7 +424,10 @@ static Parser::ControlStatus processCommand(ScilabEngineInfo* _pSEI)
             callOnPrompt();
         }
     }
-    return parser->getControlStatus();
+
+    Parser::ControlStatus ret = parser->getControlStatus();
+    delete parser;
+    return ret;
 }
 
 /*
index 49a9b25..7404a1d 100644 (file)
@@ -23,6 +23,7 @@ extern "C"
 #include <string.h>
 #include "Scierror.h"
 #include "localization.h"
+#include "sci_malloc.h"
 }
 /*--------------------------------------------------------------------------*/
 
@@ -39,6 +40,7 @@ types::Function::ReturnValue sci_pathsep(types::typed_list &in, int _iRetCount,
 
     types::String* pOut = new types::String(2, dimsArray);
     pOut->set(0, wcsSep);
+    FREE(wcsSep);
     out.push_back(pOut);
     return types::Function::OK;
 }
index ab988a7..f34fe3f 100644 (file)
@@ -85,7 +85,6 @@ static double *fileinfo_WindowsW(wchar_t* _pwstFilename, int *_piErr)
         return NULL;
     }
 
-    FILEINFO_ARRAY = (double*)MALLOC(sizeof(double) * FILEINFO_ARRAY_SIZE);
     os_swprintf(DriveTemp, PATH_MAX + FILENAME_MAX + 1, L"%ls", _pwstFilename);
     if ( (DriveTemp[wcslen(DriveTemp) - 1] == L'/') || (DriveTemp[wcslen(DriveTemp) - 1] == L'\\') )
     {
@@ -127,6 +126,7 @@ static double *fileinfo_WindowsW(wchar_t* _pwstFilename, int *_piErr)
                 else
                 {
                     *_piErr = FILEINFO_DEFAULT_ERROR;
+                    FREE(FILEINFO_ARRAY);
                     return NULL;
                 }
             }
@@ -134,6 +134,7 @@ static double *fileinfo_WindowsW(wchar_t* _pwstFilename, int *_piErr)
         else
         {
             *_piErr = result;
+            FREE(FILEINFO_ARRAY);
             return NULL;
         }
     }
@@ -160,6 +161,7 @@ static double *fileinfo_WindowsW(wchar_t* _pwstFilename, int *_piErr)
         else
         {
             *_piErr = FILEINFO_DEFAULT_ERROR;
+            FREE(FILEINFO_ARRAY);
             return NULL;
         }
     }
index 87ea85d..4f46338 100644 (file)
@@ -130,7 +130,6 @@ int ScinotesModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -153,7 +152,6 @@ int FunctionsModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -175,7 +173,6 @@ int StatisticsModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -197,7 +194,6 @@ int SignalProcessingModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -228,7 +224,6 @@ int HelptoolsModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -250,7 +245,6 @@ int MatioModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -270,7 +264,6 @@ int Hdf5Module::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -292,7 +285,6 @@ int ActionBindingModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -312,7 +304,6 @@ int SpreadsheetModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -334,7 +325,6 @@ int InterpolationModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -356,7 +346,6 @@ int SoundModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -376,7 +365,6 @@ int RandlibModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -396,7 +384,6 @@ int UmfpackModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -416,7 +403,6 @@ int OptimizationModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -436,7 +422,6 @@ int SpecialFunctionModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -456,7 +441,6 @@ int GraphicExportModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -476,7 +460,6 @@ int ArnoldiModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -496,7 +479,6 @@ int CallScilabModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -516,7 +498,6 @@ int CompletionModule::Load()
     }
 
     FREE(pwstLibName);
-
     return 1;
 }
 
@@ -535,6 +516,7 @@ int XmlModule::Load()
         symbol::Context::getInstance()->addFunction(types::Function::createFunction(vect[i].wstFunction, vect[i].wstName, pwstLibName, vect[i].iType, NULL, wstModuleName));
     }
 
+    FREE(pwstLibName);
     return 1;
 }
 
@@ -553,6 +535,7 @@ int ScicosModule::Load()
         symbol::Context::getInstance()->addFunction(types::Function::createFunction(vect[i].wstFunction, vect[i].wstName, pwstLibName, vect[i].iType, NULL, wstModuleName));
     }
 
+    FREE(pwstLibName);
     return 1;
 }
 
@@ -581,6 +564,7 @@ int XcosModule::Load()
         symbol::Context::getInstance()->addFunction(types::Function::createFunction(vect[i].wstFunction, vect[i].wstName, pwstLibName, vect[i].iType, &XcosModule::LoadDeps, wstModuleName));
     }
 
+    FREE(pwstLibName);
     return 1;
 }
 
index fd8df99..9247e7e 100644 (file)
@@ -79,11 +79,12 @@ BOOL HistoryFile::setDefaultFilename(void)
     char* SCIHOME = getSCIHOME();
     std::string stDefaultFilename;
 
-    stDefaultFilename = std::string(getSCIHOME());
+    stDefaultFilename = std::string(SCIHOME);
     stDefaultFilename += std::string(DIR_SEPARATOR);
     stDefaultFilename += std::string(DEFAULT_HISTORY_FILE);
 
     setFilename(stDefaultFilename);
+    FREE(SCIHOME);
     return TRUE;
 }
 /*------------------------------------------------------------------------*/
index e68ebe4..809ddd7 100644 (file)
@@ -139,6 +139,7 @@ int sci_getenv(char *fname, void* pvApiCtx)
     }
 
     getenvc(&ierr, pStVarOne, env_value, &length_env, &iflag);
+    freeAllocatedSingleString(pStVarOne);
 
     //create variable on stack and return it.
     if (createSingleString(pvApiCtx, Rhs + 1, env_value))
index dda2f36..5581170 100644 (file)
@@ -49,6 +49,7 @@ void getenvc(int *ierr, const char *var, char *buf, int *buflen, int *iflag)
             sciprint(_("Undefined environment variable %s.\n"), var);
         }
 
+        FREE(wvar);
         *ierr = 1;
         return;
     }
@@ -62,12 +63,15 @@ void getenvc(int *ierr, const char *var, char *buf, int *buflen, int *iflag)
                 sciprint(_("Undefined environment variable %s.\n"), var);
             }
 
+            FREE(wvar);
             *ierr = 1;
             return;
         }
     }
 
     temp = wide_string_to_UTF8(wbuf);
+    FREE(wbuf);
+    FREE(wvar);
     *buflen = (int)strlen(temp);
     if (buf)
     {
index 92c897e..5f9729f 100644 (file)
@@ -176,7 +176,7 @@ types::Library* loadlib(std::wstring _wstXML, bool _isFile, bool _bAddInContext)
     }
 
     xmlFreeDoc(doc);
-
+    FREE(pstLibName);
     return lib;
 }
 
index 7948ed5..fe18523 100644 (file)
@@ -36,6 +36,7 @@ BOOL loadOnUseClassPath(char const* tag)
     char *classpathfile = (char*)MALLOC(sizeof(char) * (strlen(sciPath) + strlen(XMLCLASSPATH) + 1));
 
     sprintf(classpathfile, XMLCLASSPATH, sciPath);
+    FREE(sciPath);
 
     // Load the XML
     if ( FileExist(classpathfile) )
@@ -64,6 +65,13 @@ BOOL loadOnUseClassPath(char const* tag)
                 FREE(XPath);
                 XPath = NULL;
             }
+
+            if (classpathfile)
+            {
+                FREE(classpathfile);
+                classpathfile = NULL;
+            }
+
             return bOK;
         }
 
index 2b0bcef..4d5833f 100644 (file)
@@ -209,6 +209,7 @@ BOOL setlanguage(const wchar_t *lang)
                 }
 #endif
 #endif
+                FREE(ret);
                 return TRUE;
             }
 #ifndef _MSC_VER
index 9c9bbc3..abc6823 100644 (file)
  *
  */
 
+//#define DEBUG_VLD
+#if defined(DEBUG_VLD)
+#include <vld.h>
+#endif
+
 #pragma comment(lib,"../../../../../bin/libintl.lib")
 
 #include <cstdio>
@@ -27,6 +32,7 @@ extern "C"
 #include "scilabRead.h"
 #include "ConsoleRead.h"
 #include "version.h"
+#include "sci_malloc.h"
 
     extern char *getCmdLine(void);
 }
@@ -283,5 +289,6 @@ int main(int argc, char *argv[])
     StartScilabEngine(pSEI);
     iRet = RunScilabEngine(pSEI);
     StopScilabEngine(pSEI);
+    FREE(pSEI);
     return iRet;
 }
index f45a6b3..5790bd9 100644 (file)
@@ -104,6 +104,7 @@ Function::ReturnValue sci_strsubst(typed_list &in, int _iRetCount, typed_list &o
         pwstOutput = wcssubst_reg(const_cast<const wchar_t**>(pS->get()), pS->getSize(), pwstSearch, pwstReplace, &iErr);
         if (iErr != NO_MATCH && iErr != PCRE_FINISHED_OK && iErr != PCRE_EXIT)
         {
+            freeArrayOfWideString(pwstOutput, pOut->getSize());
             pcre_error("strsubst", iErr);
             delete pOut;
             return Function::Error;
index 7cbd733..a676a03 100644 (file)
@@ -111,7 +111,7 @@ types::Function::ReturnValue sci_tokens(types::typed_list &in, int _iRetCount, t
     int dims = 2;
 
     wchar_t** Output_Strings = stringTokens(pString->get(0), seps, &dimsArray[0]);
-
+    FREE(seps);
     if (Output_Strings == NULL)
     {
         //return empty matrix
index 194cadc..7136584 100644 (file)
@@ -373,7 +373,7 @@ wchar_t *wcssub(const wchar_t* _pwstInput, const wchar_t* _pwstSearch, const wch
 
     pwstOutput = (wchar_t*)MALLOC(sizeof(wchar_t) * (wcslen(_pwstInput) + iOffset + 1));
     memset(pwstOutput, 0x00, sizeof(wchar_t) * (wcslen(_pwstInput) + iOffset + 1));
-
+    
     if (iOccurs == 0)
     {
         wcscpy(pwstOutput, _pwstInput);
@@ -399,6 +399,7 @@ wchar_t *wcssub(const wchar_t* _pwstInput, const wchar_t* _pwstSearch, const wch
         wcscpy(pwstOutput + wcslen(pwstOutput), _pwstInput + piStart[iOccurs - 1] + iSearch);
     }
 
+    FREE(piStart);
     return pwstOutput;
 }
 /*-------------------------------------------------------------------------------------*/
index 39e3fb5..8d5fcda 100644 (file)
@@ -157,10 +157,12 @@ struct Libraries
             {
                 if (it->second->top()->m_iLevel == _iLevel)
                 {
-                    types::Library* pIT = it->second->top()->m_pLib;
+                    ScopedLibrary * pSL = it->second->top();
+                    types::Library* pIT = pSL->m_pLib;
                     pIT->DecreaseRef();
                     pIT->killMe();
                     it->second->pop();
+                    delete pSL;
                     return true;
                 }
             }
@@ -173,7 +175,8 @@ struct Libraries
     {
         std::list<std::wstring>* names = new std::list<std::wstring>();
         MapLibs::iterator it = libs.begin();
-        for (; it != libs.end() ; ++it)
+        MapLibs::iterator itEnd = libs.end();
+        for (; it != itEnd ; ++it)
         {
             std::list<std::wstring>* temp = it->second->getMacrosName();
             names->insert(names->end(), temp->begin(), temp->end());
index eb4f9f5..f20190a 100644 (file)
@@ -35,6 +35,24 @@ struct EXTERN_SYMBOL ScopedVariable
 struct EXTERN_SYMBOL Variable
 {
     Variable(const Symbol& _name) : name(_name), m_Global(false), m_GlobalValue(NULL) {};
+    ~Variable()
+    {
+        while (!empty())
+        {
+            ScopedVariable * pSV = top();
+            types::InternalType * pIT = pSV->m_pIT;
+            pIT->DecreaseRef();
+            pIT->killMe();
+            pop();
+            delete pSV;
+        }
+
+        if (m_GlobalValue)
+        {
+            m_GlobalValue->DecreaseRef();
+            m_GlobalValue->killMe();
+        }
+    }
 
     void put(types::InternalType* _pIT, int _iLevel)
     {
@@ -235,6 +253,7 @@ struct Variables
                 it->second->pop();
                 types::InternalType* pIT = getAllButCurrentLevel(_key, _iLevel);
                 it->second->put(pSave->m_pIT, pSave->m_iLevel);
+                delete pSave;
                 return pIT;
             }
         }
@@ -248,10 +267,12 @@ struct Variables
         {
             if (_var->top()->m_iLevel == _iLevel)
             {
-                types::InternalType* pIT = _var->top()->m_pIT;
+                ScopedVariable* pSave = _var->top();
+                types::InternalType* pIT = pSave->m_pIT;
                 pIT->DecreaseRef();
                 pIT->killMe();
                 _var->pop();
+                delete pSave;
             }
         }
 
@@ -326,6 +347,7 @@ struct Variables
             _var->pop();
             putInPreviousScope(_var, _pIT, _iLevel);
             _var->put(pVar->m_pIT, pVar->m_iLevel);
+            delete pVar;
         }
         else
         {
@@ -383,16 +405,6 @@ struct Variables
     {
         for (MapVars::iterator it = vars.begin(); it != vars.end() ; ++it)
         {
-            while (!it->second->empty())
-            {
-                ScopedVariable * pSV = it->second->top();
-                types::InternalType * pIT = pSV->m_pIT;
-                pIT->DecreaseRef();
-                pIT->killMe();
-                it->second->pop();
-                delete pSV;
-            }
-
             delete it->second;
         }
     }
index bf2e1e8..e263159 100644 (file)
@@ -435,6 +435,7 @@ Callable::ReturnValue DynamicFunction::Init()
 
     char* pstLibName = wide_string_to_UTF8(m_wstLibName.c_str());
     m_hLib = LoadDynLibrary(pstLibName);
+    FREE(pstLibName);
     if (m_hLib == NULL)
     {
         //2nd chance for linux !
@@ -488,22 +489,24 @@ Callable::ReturnValue DynamicFunction::Init()
     /*Load gateway*/
     if (m_wstName != L"")
     {
-        char* _pstEntryPoint = wide_string_to_UTF8(m_wstEntryPoint.c_str());
+        char* pstEntryPoint = wide_string_to_UTF8(m_wstEntryPoint.c_str());
         switch (m_iType)
         {
             case EntryPointCPPOpt :
-                m_pOptFunc = (GW_FUNC_OPT)GetDynLibFuncPtr(m_hLib, _pstEntryPoint);
+                m_pOptFunc = (GW_FUNC_OPT)GetDynLibFuncPtr(m_hLib, pstEntryPoint);
                 break;
             case EntryPointCPP :
-                m_pFunc = (GW_FUNC)GetDynLibFuncPtr(m_hLib, _pstEntryPoint);
+                m_pFunc = (GW_FUNC)GetDynLibFuncPtr(m_hLib, pstEntryPoint);
                 break;
             case EntryPointC :
-                m_pOldFunc = (OLDGW_FUNC)GetDynLibFuncPtr(m_hLib, _pstEntryPoint);
+                m_pOldFunc = (OLDGW_FUNC)GetDynLibFuncPtr(m_hLib, pstEntryPoint);
                 break;
             case EntryPointMex :
-                m_pMexFunc = (MEXGW_FUNC)GetDynLibFuncPtr(m_hLib, _pstEntryPoint);
+                m_pMexFunc = (MEXGW_FUNC)GetDynLibFuncPtr(m_hLib, pstEntryPoint);
                 break;
         }
+
+        FREE(pstEntryPoint);
     }
 
     if (m_pFunc == NULL && m_pOldFunc == NULL && m_pMexFunc == NULL && m_pOptFunc == NULL)
index 3300b9c..9869012 100644 (file)
@@ -54,6 +54,20 @@ Macro::Macro(const std::wstring& _stName, std::list<symbol::Variable*>& _inputAr
 Macro::~Macro()
 {
     delete m_body;
+    m_pDblArgIn->DecreaseRef();
+    m_pDblArgIn->killMe();
+    m_pDblArgOut->DecreaseRef();
+    m_pDblArgOut->killMe();
+
+    if (m_inputArgs)
+    {
+        delete m_inputArgs;
+    }
+
+    if (m_outputArgs)
+    {
+        delete m_outputArgs;
+    }
 }
 
 InternalType* Macro::clone()
@@ -179,8 +193,24 @@ Callable::ReturnValue Macro::call(typed_list &in, optional_list &opt, int _iRetC
     //common part with or without varargin/varargout
 
     // Declare nargin & nargout in function context.
+    if (m_pDblArgIn->getRef() > 1)
+    {
+        m_pDblArgIn->DecreaseRef();
+        m_pDblArgIn = (Double*)m_pDblArgIn->clone();
+        m_pDblArgIn->IncreaseRef();
+    }
+
     m_pDblArgIn->set(0, static_cast<double>(in.size()));
+
+    if (m_pDblArgOut->getRef() > 1)
+    {
+        m_pDblArgOut->DecreaseRef();
+        m_pDblArgOut = (Double*)m_pDblArgOut->clone();
+        m_pDblArgOut->IncreaseRef();
+    }
+
     m_pDblArgOut->set(0, _iRetCount);
+
     pContext->put(m_Nargin, m_pDblArgIn);
     pContext->put(m_Nargout, m_pDblArgOut);
 
index 2cf7f55..a3f4438 100644 (file)
@@ -74,19 +74,21 @@ Callable::ReturnValue MacroFile::call(typed_list &in, optional_list &opt, int _i
 
 bool MacroFile::parse(void)
 {
-
     if (m_pMacro == NULL)
     {
         //load file, only for the first call
-        std::ifstream f(wide_string_to_UTF8(m_stPath.c_str()), ios::in | ios::binary | ios::ate);
+        char* pstPath = wide_string_to_UTF8(m_stPath.c_str());
+        std::ifstream f(pstPath, ios::in | ios::binary | ios::ate);
+        FREE(pstPath);
 
         int size = (int)f.tellg();
         unsigned char* binAst = new unsigned char[size];
         f.seekg(0);
         f.read((char*)binAst, size);
         f.close();
-        ast::DeserializeVisitor* d = new ast::DeserializeVisitor(binAst);
-        ast::Exp* tree = d->deserialize();
+        ast::DeserializeVisitor d(binAst);
+        ast::Exp* tree = d.deserialize();
+        delete[] binAst;
 
         //find FunctionDec
         ast::FunctionDec* pFD = NULL;
@@ -129,7 +131,15 @@ bool MacroFile::parse(void)
                     }
                 }
             }
+            else
+            {
+                delete *j;
+            }
         }
+
+        ((ast::SeqExp*)tree)->exps_clear();
+        delete tree;
+
     }
     return true;
 }
index 9ddca76..6059955 100644 (file)
@@ -66,7 +66,9 @@ String::String(const char *_pstData)
     wchar_t** pwsData = NULL;
     int piDims[] = {1, 1};
     create(piDims, 2, &pwsData, NULL);
-    set(0, 0, to_wide_string(const_cast<char*>(_pstData)));
+    wchar_t* data = to_wide_string(const_cast<char*>(_pstData));
+    set(0, 0, data);
+    FREE(data);
 #ifndef NDEBUG
     Inspector::addItem(this);
 #endif