Fix some memory leaks 85/10085/5
Sylvestre Ledru [Wed, 26 Dec 2012 08:37:26 +0000 (09:37 +0100)]
Change-Id: I71b956a23a96c35f83ed9529938cf3cb7bd8e413

scilab/modules/core/src/c/getversion.c
scilab/modules/fileio/sci_gateway/c/sci_isdir.c
scilab/modules/fileio/sci_gateway/c/sci_isfile.c
scilab/modules/gui/sci_gateway/c/sci_ClipBoard.c
scilab/modules/jvm/src/c/getJvmOptions.c
scilab/modules/matio/src/c/GetSparseVariable.c
scilab/modules/ui_data/src/cpp/UpdateBrowseVar.cpp

index 7ec2dec..601016b 100644 (file)
@@ -20,6 +20,7 @@
 #endif
 #include "with_module.h"
 #include "loadversion.h"
+#include "freeArrayOfString.h"
 #include "MALLOC.h"
 #include "../../../io/includes/getenvc.h"
 /*--------------------------------------------------------------------------*/
@@ -181,7 +182,7 @@ char **getScilabVersionOptions(int *sizeArrayReturned)
             }
             else
             {
-                FREE(options);
+                freeArrayOfString(options, nbOptions);
                 return NULL;
             }
         }
@@ -196,7 +197,7 @@ char **getScilabVersionOptions(int *sizeArrayReturned)
             }
             else
             {
-                FREE(options);
+                freeArrayOfString(options, nbOptions);
                 return NULL;
             }
         }
@@ -209,7 +210,7 @@ char **getScilabVersionOptions(int *sizeArrayReturned)
         }
         else
         {
-            FREE(options);
+            freeArrayOfString(options, nbOptions);
             return NULL;
         }
 
@@ -221,7 +222,7 @@ char **getScilabVersionOptions(int *sizeArrayReturned)
         }
         else
         {
-            FREE(options);
+            freeArrayOfString(options, nbOptions);
             return NULL;
         }
 
@@ -233,7 +234,7 @@ char **getScilabVersionOptions(int *sizeArrayReturned)
         }
         else
         {
-            FREE(options);
+            freeArrayOfString(options, nbOptions);
             return NULL;
         }
 
index 434352f..25624ba 100644 (file)
@@ -105,6 +105,7 @@ int sci_isdir(char *fname, unsigned long fname_len)
     if (pStVarOne == NULL)
     {
         FREE(lenStVarOne);
+        FREE(results);
         lenStVarOne = NULL;
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
         return 0;
@@ -115,12 +116,20 @@ int sci_isdir(char *fname, unsigned long fname_len)
         pStVarOne[i] = (wchar_t*)MALLOC(sizeof(wchar_t) * (lenStVarOne[i] + 1));
         if (pStVarOne[i] == NULL)
         {
-            freeArrayOfWideString(pStVarOne, m1 * n1);
+            if (i == 0)
+            {
+                FREE(pStVarOne);
+            }
+            else
+            {
+                freeArrayOfWideString(pStVarOne, i - 1);
+            }
             if (lenStVarOne)
             {
                 FREE(lenStVarOne);
                 lenStVarOne = NULL;
             }
+            FREE(results);
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return 0;
         }
index b7ba1aa..d60aa55 100644 (file)
@@ -82,7 +82,6 @@ int sci_isfile(char *fname, unsigned long fname_len)
             FREE(lenStVarOne);
             lenStVarOne = NULL;
         }
-        freeArrayOfWideString(pStVarOne, m1 * n1);
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
         return 0;
     }
@@ -104,6 +103,7 @@ int sci_isfile(char *fname, unsigned long fname_len)
             FREE(lenStVarOne);
             lenStVarOne = NULL;
         }
+        FREE(results);
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
         return 0;
     }
@@ -113,12 +113,20 @@ int sci_isfile(char *fname, unsigned long fname_len)
         pStVarOne[i] = (wchar_t*)MALLOC(sizeof(wchar_t) * (lenStVarOne[i] + 1));
         if (pStVarOne[i] == NULL)
         {
-            freeArrayOfWideString(pStVarOne, m1 * n1);
+            if (i == 0)
+            {
+                FREE(pStVarOne);
+            }
+            else
+            {
+                freeArrayOfWideString(pStVarOne, i - 1);
+            }
             if (lenStVarOne)
             {
                 FREE(lenStVarOne);
                 lenStVarOne = NULL;
             }
+            FREE(results);
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return 0;
         }
@@ -129,6 +137,8 @@ int sci_isfile(char *fname, unsigned long fname_len)
     {
         printError(&sciErr, 0);
         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
+        freeArrayOfWideString(pStVarOne, m1*n1);
+        FREE(results);
         return 0;
     }
 
@@ -159,6 +169,7 @@ int sci_isfile(char *fname, unsigned long fname_len)
     {
         printError(&sciErr, 0);
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
+        free(results);
         return 0;
     }
 
index 1a5af2e..4434a87 100644 (file)
@@ -3,11 +3,11 @@
 * Copyright (C) 2005 - INRIA - Allan CORNET
 * Copyright (C) 2008 - INRIA - Vincent COUVERT
 * Copyright (C) 2010 - DIGITEO - Vincent COUVERT
-* 
+*
 * This file must be used under the terms of the CeCILL.
 * This source file is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
-* are also available at    
+* are also available at
 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 *
 */
 #include "strdup_windows.h"
 #endif
 /*--------------------------------------------------------------------------*/
-int sci_ClipBoard(char *fname,unsigned long l)
+int sci_ClipBoard(char *fname, unsigned long l)
 {
-    static int l1 = 0,n1 = 0,m1 = 0;
+    static int l1 = 0, n1 = 0, m1 = 0;
     char *param1 = NULL, *param2 = NULL;
 
-    Rhs=Max(0,Rhs);
-    CheckRhs(0,2);
-    CheckLhs(0,1);
+    Rhs = Max(0, Rhs);
+    CheckRhs(0, 2);
+    CheckLhs(0, 1);
 
     if ( getScilabMode() != SCILAB_NWNI )
     {
@@ -45,12 +45,12 @@ int sci_ClipBoard(char *fname,unsigned long l)
         /*--------------------*/
         if (Rhs == 1)
         {
-            if (GetType(1)==sci_strings)
+            if (GetType(1) == sci_strings)
             {
-                GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
-                param1=cstk(l1);
+                GetRhsVar(1, STRING_DATATYPE, &m1, &n1, &l1);
+                param1 = cstk(l1);
 
-                if ( ( strcmp(param1,"paste") == 0 ) || ( strcmp(param1,"pastespecial") == 0 ) )
+                if ( ( strcmp(param1, "paste") == 0 ) || ( strcmp(param1, "pastespecial") == 0 ) )
                 {
                     /* Use the Java clipboard (CallScilabBridge.java returns "" if clipbaord could not be read) */
                     char *output = getClipboardContents();
@@ -59,42 +59,42 @@ int sci_ClipBoard(char *fname,unsigned long l)
                     m1 = (int)strlen(output);
                     n1 = 1;
 
-                    CreateVarFromPtr(Rhs+ 1,STRING_DATATYPE, &m1, &n1, &output);
+                    CreateVarFromPtr(Rhs + 1, STRING_DATATYPE, &m1, &n1, &output);
                     /* TO DO a delete [] and not a FREE */
                     FREE(output);
-                    LhsVar(1)=Rhs+1;
+                    LhsVar(1) = Rhs + 1;
 
-                    PutLhsVar();       
+                    PutLhsVar();
                     return TRUE;
                 }
                 else
                 {
-                    Scierror(999,_("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, "paste", "pastespecial");
+                    Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, "paste", "pastespecial");
                     return FALSE;
                 }
             }
             else
             {
-                Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
+                Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
                 return FALSE;
             }
-        } 
+        }
 
         else if (Rhs == 2)
         {
-            if (GetType(1)==sci_strings)
+            if (GetType(1) == sci_strings)
             {
                 /* Get the first argument: should be "copy" or "do" */
-                GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
-                param1=cstk(l1);
+                GetRhsVar(1, STRING_DATATYPE, &m1, &n1, &l1);
+                param1 = cstk(l1);
 
-                if (n1!=1)
+                if (n1 != 1)
                 {
-                    Scierror(999,_("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, "do","copy");
+                    Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, "do", "copy");
                     return FALSE;
                 }
 
-                if (GetType(2)==sci_strings)
+                if (GetType(2) == sci_strings)
                 {
 
                     /*-------------------------------------------*/
@@ -102,37 +102,37 @@ int sci_ClipBoard(char *fname,unsigned long l)
                     /*-------------------------------------------*/
 
                     /* @TODO : should be remplaced by an enum */
-                    if ( strcmp(param1,"do") == 0 )
+                    if ( strcmp(param1, "do") == 0 )
                     {
-                        GetRhsVar(2,STRING_DATATYPE,&m1,&n1,&l1);
-                        param2=cstk(l1);
+                        GetRhsVar(2, STRING_DATATYPE, &m1, &n1, &l1);
+                        param2 = cstk(l1);
 
-                        if ( strcmp(param2,"paste") == 0 )
+                        if ( strcmp(param2, "paste") == 0 )
                         {
                             /* Call Java to do the job */
                             pasteClipboardIntoConsole();
                         }
-                        else if ( strcmp(param2,"copy") == 0 )
+                        else if ( strcmp(param2, "copy") == 0 )
                         {
                             /* Call Java to do the job */
                             copyConsoleSelection();
                         }
-                        else if ( strcmp(param2,"empty") == 0 )
+                        else if ( strcmp(param2, "empty") == 0 )
                         {
                             /* Call Java to do the job */
                             emptyClipboard();
                         }
                         else
                         {
-                            Scierror(999,_("%s: Wrong value for input argument #%d: '%s', '%s' or '%s' expected.\n"), fname, 2, "copy","paste","empty");
+                            Scierror(999, _("%s: Wrong value for input argument #%d: '%s', '%s' or '%s' expected.\n"), fname, 2, "copy", "paste", "empty");
                             return FALSE;
                         }
 
-                        m1=0;
-                        n1=0;
-                        CreateVar(Rhs+1,MATRIX_OF_DOUBLE_DATATYPE,  &m1, &n1, &l1);
-                        LhsVar(1)=Rhs+1;
-                        PutLhsVar();   
+                        m1 = 0;
+                        n1 = 0;
+                        CreateVar(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE,  &m1, &n1, &l1);
+                        LhsVar(1) = Rhs + 1;
+                        PutLhsVar();
                         return TRUE;
                     }
 
@@ -140,7 +140,7 @@ int sci_ClipBoard(char *fname,unsigned long l)
                     /* clipboard("copy", data) */
                     /*-------------------------*/
 
-                    else if ( strcmp(param1,"copy") == 0 )
+                    else if ( strcmp(param1, "copy") == 0 )
                     {
                         char *TextToPutInClipboard = NULL;
                         char **Str = NULL;
@@ -150,7 +150,7 @@ int sci_ClipBoard(char *fname,unsigned long l)
 
                         if (m2 * n2 == 1) /* Single line copy */
                         {
-                            TextToPutInClipboard=Str[0];
+                            TextToPutInClipboard = Str[0];
                             /* Call Java to do the job */
                             setClipboardContents(TextToPutInClipboard);
                         }
@@ -159,67 +159,71 @@ int sci_ClipBoard(char *fname,unsigned long l)
                             int i = 0, j = 0, l2 = 0;
                             char *TextToSendInClipboard = NULL;
                             int SizeofTextToSendInClipboard = 0;
-                            char **buffer = (char**)MALLOC( (m2 * n2)*sizeof(char *) );
+                            char **buffer = (char**)MALLOC( (m2 * n2) * sizeof(char *) );
                             if (buffer == NULL)
                             {
                                 Scierror(999, _("%s: No more memory.\n"), fname);
                                 return FALSE;
                             }
 
-                            for (i=0; i<m2; i++) 
+                            for (i = 0; i < m2; i++)
                             {
-                                for (j=0; j<n2; j++) 
+                                for (j = 0; j < n2; j++)
                                 {
-                                    SizeofTextToSendInClipboard = SizeofTextToSendInClipboard + (int)strlen(Str[j*m2+i]) + (int)strlen("\n") + (int)strlen(" ");
-                                    buffer[i*n2+j] = strdup(Str[j*m2+i]);
+                                    SizeofTextToSendInClipboard = SizeofTextToSendInClipboard + (int)strlen(Str[j * m2 + i]) + (int)strlen("\n") + (int)strlen(" ");
+                                    buffer[i * n2 + j] = strdup(Str[j * m2 + i]);
                                 }
                             }
 
-                            TextToSendInClipboard = (char*)MALLOC( (SizeofTextToSendInClipboard)*sizeof(char) );
+                            TextToSendInClipboard = (char*)MALLOC( (SizeofTextToSendInClipboard) * sizeof(char) );
                             if (TextToSendInClipboard == NULL)
                             {
                                 Scierror(999, _("%s: No more memory.\n"), fname);
+                                freeArrayOfString(buffer, m2 * n2);
+
                                 return FALSE;
                             }
                             strcpy(TextToSendInClipboard, "");
 
                             for (i = 0; i < m2; i++)
                             {
-                                for (j = 0; j < n2; j++) 
+                                for (j = 0; j < n2; j++)
                                 {
                                     strcat(TextToSendInClipboard, buffer[l2++]);
-                                    strcat(TextToSendInClipboard," ");
+                                    strcat(TextToSendInClipboard, " ");
+                                }
+                                if ( i != (m2 - 1) )
+                                {
+                                    strcat(TextToSendInClipboard, "\n");
                                 }
-                                if ( i != (m2 - 1) ) strcat(TextToSendInClipboard, "\n");
                             }
 
                             /* Call Java to do the job */
                             setClipboardContents(TextToSendInClipboard);
 
-                            FREE(buffer);
-                            buffer=NULL;
+                            freeArrayOfString(buffer, m2 * n2);
 
                             FREE(TextToSendInClipboard);
-                            TextToSendInClipboard=NULL;
+                            TextToSendInClipboard = NULL;
                         }
 
-                        freeArrayOfString(Str,m2*n2);
+                        freeArrayOfString(Str, m2 * n2);
                         m1 = 0;
                         n1 = 0;
-                        CreateVar(Rhs+1, MATRIX_OF_DOUBLE_DATATYPE, &m1, &n1, &l1);
-                        LhsVar(1) = Rhs+1;
-                        PutLhsVar();           
+                        CreateVar(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE, &m1, &n1, &l1);
+                        LhsVar(1) = Rhs + 1;
+                        PutLhsVar();
                         return TRUE;
                     }
                     else
                     {
-                        Scierror(999,_("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, "do","copy");
+                        Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, "do", "copy");
                         return FALSE;
                     }
                 }
                 else
                 {
-                    Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
+                    Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
                     return FALSE;
                 }
             }
@@ -228,31 +232,31 @@ int sci_ClipBoard(char *fname,unsigned long l)
             /* clipboard(fignum, {"EMF","DIB"}) */
             /*----------------------------------*/
 
-            else if (GetType(1)==sci_matrix)
+            else if (GetType(1) == sci_matrix)
             {
 
-                int num_win=-2;
+                int num_win = -2;
 
-                GetRhsVar(1,MATRIX_OF_INTEGER_DATATYPE,&m1,&n1,&l1);
-                num_win=*istk(l1);
+                GetRhsVar(1, MATRIX_OF_INTEGER_DATATYPE, &m1, &n1, &l1);
+                num_win = *istk(l1);
 
-                if (m1*n1!=1)
+                if (m1*n1 != 1)
                 {
-                    Scierror(999,_("%s: Wrong size for input argument #%d: A real expected.\n"), fname, 1);
+                    Scierror(999, _("%s: Wrong size for input argument #%d: A real expected.\n"), fname, 1);
                     return FALSE;
                 }
 
-                if (GetType(2)==sci_strings)
+                if (GetType(2) == sci_strings)
                 {
-                    GetRhsVar(2,STRING_DATATYPE,&m1,&n1,&l1);
-                    param2=cstk(l1);
+                    GetRhsVar(2, STRING_DATATYPE, &m1, &n1, &l1);
+                    param2 = cstk(l1);
 
-                    if ( ( strcmp(param2,"EMF") == 0 ) || ( strcmp(param2,"DIB") == 0 ) )
+                    if ( ( strcmp(param2, "EMF") == 0 ) || ( strcmp(param2, "DIB") == 0 ) )
                     {
-                        if (num_win>=0)
+                        if (num_win >= 0)
                         {
                             /* Call Java */
-                            if ( strcmp(param2,"EMF") == 0)
+                            if ( strcmp(param2, "EMF") == 0)
                             {
                                 /* @TODO create EMF */
                                 copyFigureToClipBoard(num_win);
@@ -263,43 +267,43 @@ int sci_ClipBoard(char *fname,unsigned long l)
                                 copyFigureToClipBoard(num_win);
                             }
 
-                            m1=0;
-                            n1=0;
-                            CreateVar(Rhs+1,MATRIX_OF_DOUBLE_DATATYPE,  &m1, &n1, &l1);
-                            LhsVar(1)=Rhs+1;
-                            PutLhsVar();               
+                            m1 = 0;
+                            n1 = 0;
+                            CreateVar(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE,  &m1, &n1, &l1);
+                            LhsVar(1) = Rhs + 1;
+                            PutLhsVar();
                             return TRUE;
                         }
                         else
                         {
-                            Scierror(999,_("%s: Wrong value for input argument #%d: Must be >= %d expected.\n"), fname, 1, 0);
+                            Scierror(999, _("%s: Wrong value for input argument #%d: Must be >= %d expected.\n"), fname, 1, 0);
                             return FALSE;
                         }
 
                     }
                     else
                     {
-                        Scierror(999,_("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 2, "EMF","DIB");
+                        Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 2, "EMF", "DIB");
                         return FALSE;
                     }
 
                 }
                 else
                 {
-                    Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
+                    Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
                     return FALSE;
                 }
             }
             else
             {
-                Scierror(999,_("%s: Wrong type for input argument #%d: A string or a real expected.\n"), fname, 1);
+                Scierror(999, _("%s: Wrong type for input argument #%d: A string or a real expected.\n"), fname, 1);
                 return FALSE;
             }
         }
     }
     else
     {
-        Scierror(999,_("%s: Function not available in NWNI mode.\n"), fname);
+        Scierror(999, _("%s: Function not available in NWNI mode.\n"), fname);
         return FALSE;
     }
 
index f142f07..4620021 100644 (file)
@@ -49,7 +49,6 @@ JavaVMOption * getJvmOptions(char *SCI_PATH, char *filename_xml_conf, int *size_
             xmlXPathObjectPtr xpathObj = NULL;
             char *jvm_option_string = NULL;
             char *xpath_query = NULL;
-            char *heapSize = getJavaHeapSize();
 
             int indice = 0;
             {
@@ -71,7 +70,6 @@ JavaVMOption * getJvmOptions(char *SCI_PATH, char *filename_xml_conf, int *size_
                     FREE(encoding);
                     encoding = NULL;
                 }
-                FREE(heapSize);
                 *size_JavaVMOption = 0;
                 return NULL;
             }
@@ -90,6 +88,8 @@ JavaVMOption * getJvmOptions(char *SCI_PATH, char *filename_xml_conf, int *size_
                 /* the Xpath has been understood and there are node */
                 int i;
                 char heapSizeUsed = 0;
+                char *heapSize = getJavaHeapSize();
+
                 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
                 {
 
@@ -146,8 +146,14 @@ JavaVMOption * getJvmOptions(char *SCI_PATH, char *filename_xml_conf, int *size_
                 }
             }
 
-            if (xpathObj) xmlXPathFreeObject(xpathObj);
-            if (xpathCtxt) xmlXPathFreeContext(xpathCtxt);
+            if (xpathObj)
+            {
+                xmlXPathFreeObject(xpathObj);
+            }
+            if (xpathCtxt)
+            {
+                xmlXPathFreeContext(xpathCtxt);
+            }
             xmlFreeDoc (doc);
 
             /* xmlCleanupParser is called in
index b391f98..2025678 100644 (file)
@@ -1,11 +1,11 @@
 /*
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
- * Copyright (C) 2008 - INRIA - Vincent COUVERT 
- * 
+ * Copyright (C) 2008 - INRIA - Vincent COUVERT
+ *
  * This file must be used under the terms of the CeCILL.
  * This source file is licensed as described in the file COPYING, which
  * you should have received as part of this distribution.  The terms
- * are also available at    
+ * are also available at
  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  *
  */
 
 matvar_t *GetSparseVariable(int iVar, const char *name, int * parent, int item_position)
 {
-  int K = 0;
-  int rank = 0;
-  int *dims = NULL;
-  double *data = NULL;
-  matvar_t *createdVar = NULL;
-  sparse_t *sparseData = NULL;
-  SciSparse scilabSparse;
-  int *colIndexes = NULL;
-  int *rowIndexes = NULL;
-  int * var_addr = NULL;
-  int * item_addr = NULL;
-  int var_type;
-  SciErr _SciErr;
-
-  if (parent==NULL)
+    int K = 0;
+    int rank = 0;
+    int *dims = NULL;
+    double *data = NULL;
+    matvar_t *createdVar = NULL;
+    sparse_t *sparseData = NULL;
+    SciSparse scilabSparse;
+    int *colIndexes = NULL;
+    int *rowIndexes = NULL;
+    int * var_addr = NULL;
+    int * item_addr = NULL;
+    int var_type;
+    SciErr _SciErr;
+
+    if (parent == NULL)
     {
-      _SciErr = getVarAddressFromPosition(pvApiCtx, iVar, &var_addr); MATIO_ERROR;
-      _SciErr = getVarType(pvApiCtx, var_addr, &var_type); MATIO_ERROR;
+        _SciErr = getVarAddressFromPosition(pvApiCtx, iVar, &var_addr);
+        MATIO_ERROR;
+        _SciErr = getVarType(pvApiCtx, var_addr, &var_type);
+        MATIO_ERROR;
     }
-  else
+    else
     {
-      _SciErr = getListItemAddress(pvApiCtx, parent, item_position, &item_addr); MATIO_ERROR;
-      _SciErr = getVarType(pvApiCtx, item_addr, &var_type); MATIO_ERROR;
+        _SciErr = getListItemAddress(pvApiCtx, parent, item_position, &item_addr);
+        MATIO_ERROR;
+        _SciErr = getVarType(pvApiCtx, item_addr, &var_type);
+        MATIO_ERROR;
     }
 
-  if (var_type == sci_sparse)
+    if (var_type == sci_sparse)
     {
-      sparseData = (sparse_t*) MALLOC(sizeof(sparse_t));
-      if (sparseData==NULL)
+        sparseData = (sparse_t*) MALLOC(sizeof(sparse_t));
+        if (sparseData == NULL)
+        {
+            Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
+            return FALSE;
+        }
+
+        rank = 2;
+        if ((dims = (int*) MALLOC (sizeof(int) * rank)) == NULL)
         {
-          Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
-          return FALSE;
+            Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
+            free(sparseData);
+            return NULL;
+        }
+
+        if (isVarComplex(pvApiCtx, var_addr))
+        {
+            if (parent == NULL)
+            {
+                getAllocatedComplexSparseMatrix(pvApiCtx, var_addr, &dims[1], &dims[0],
+                                                &scilabSparse.nel, &scilabSparse.mnel,
+                                                &scilabSparse.icol, &scilabSparse.R,
+                                                &scilabSparse.I);
+            }
+            else
+            {
+                _SciErr = getSparseMatrixInList(pvApiCtx, parent, item_position, &dims[1], &dims[0],
+                                                &scilabSparse.nel, NULL, NULL, NULL);
+                scilabSparse.mnel = (int *)MALLOC(dims[1] * sizeof(int));
+                if (scilabSparse.mnel == NULL)
+                {
+                    Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
+
+                    FREE(scilabSparse.mnel);
+
+                    return FALSE;
+                }
+                scilabSparse.icol = (int *)MALLOC(scilabSparse.nel * sizeof(int));
+                if (scilabSparse.icol == NULL)
+                {
+                    Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
+
+                    FREE(scilabSparse.mnel);
+                    FREE(scilabSparse.icol);
+
+                    return FALSE;
+                }
+                scilabSparse.R    = (double *)MALLOC(scilabSparse.nel * sizeof(double));
+                if (scilabSparse.R == NULL)
+                {
+                    Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
+
+                    FREE(scilabSparse.mnel);
+                    FREE(scilabSparse.icol);
+                    FREE(scilabSparse.R);
+
+                    return FALSE;
+                }
+                scilabSparse.I    = (double *)MALLOC(scilabSparse.nel * sizeof(double));
+                if (scilabSparse.I == NULL)
+                {
+                    Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
+
+                    FREE(scilabSparse.mnel);
+                    FREE(scilabSparse.icol);
+                    FREE(scilabSparse.R);
+                    FREE(scilabSparse.I);
+
+                    return FALSE;
+                }
+                _SciErr = getComplexSparseMatrixInList(pvApiCtx, parent, item_position, &dims[1], &dims[0],
+                                                       &scilabSparse.nel, &scilabSparse.mnel,
+                                                       &scilabSparse.icol, &scilabSparse.R, &scilabSparse.I);
+            }
+
+            scilabSparse.it = 1;
+
         }
-      
-      rank = 2;
-      if ((dims = (int*) MALLOC (sizeof(int) * rank)) == NULL)
+        else
         {
-          Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
-          return NULL;
+            if (parent == NULL)
+            {
+                getAllocatedSparseMatrix(pvApiCtx, var_addr, &dims[1], &dims[0],
+                                         &scilabSparse.nel, &scilabSparse.mnel,
+                                         &scilabSparse.icol, &scilabSparse.R);
+            }
+            else
+            {
+                _SciErr = getSparseMatrixInList(pvApiCtx, parent, item_position, &dims[1], &dims[0],
+                                                &scilabSparse.nel, NULL, NULL, NULL);
+                scilabSparse.mnel = (int *)MALLOC(dims[1] * sizeof(int));
+                if (scilabSparse.mnel == NULL)
+                {
+                    Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
+
+                    FREE(scilabSparse.mnel);
+
+                    return FALSE;
+                }
+                scilabSparse.icol = (int *)MALLOC(scilabSparse.nel * sizeof(int));
+                if (scilabSparse.icol == NULL)
+                {
+                    Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
+
+                    FREE(scilabSparse.mnel);
+                    FREE(scilabSparse.icol);
+
+                    return FALSE;
+                }
+                scilabSparse.R    = (double *)MALLOC(scilabSparse.nel * sizeof(double));
+                if (scilabSparse.R == NULL)
+                {
+                    Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
+
+                    FREE(scilabSparse.mnel);
+                    FREE(scilabSparse.icol);
+                    FREE(scilabSparse.R);
+
+                    return FALSE;
+                }
+                _SciErr = getSparseMatrixInList(pvApiCtx, parent, item_position, &dims[1], &dims[0],
+                                                &scilabSparse.nel, &scilabSparse.mnel,
+                                                &scilabSparse.icol, &scilabSparse.R);
+            }
+
+            scilabSparse.it = 0;
         }
 
-      if (isVarComplex(pvApiCtx, var_addr))
-       {
-         if (parent==NULL)
-           {
-             getAllocatedComplexSparseMatrix(pvApiCtx, var_addr, &dims[1], &dims[0], 
-                                             &scilabSparse.nel, &scilabSparse.mnel, 
-                                             &scilabSparse.icol, &scilabSparse.R, 
-                                             &scilabSparse.I);
-           }
-         else
-           {
-             _SciErr = getSparseMatrixInList(pvApiCtx, parent, item_position, &dims[1], &dims[0], 
-                                             &scilabSparse.nel, NULL, NULL, NULL);
-             scilabSparse.mnel = (int *)MALLOC(dims[1]*sizeof(int));
-             if (scilabSparse.mnel==NULL)
-               {
-                 Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
-
-                 FREE(scilabSparse.mnel);
-
-                 return FALSE;
-               }
-             scilabSparse.icol = (int *)MALLOC(scilabSparse.nel*sizeof(int));
-             if (scilabSparse.icol==NULL)
-               {
-                 Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
-
-                 FREE(scilabSparse.mnel);
-                 FREE(scilabSparse.icol);
-
-                 return FALSE;
-               }
-             scilabSparse.R    = (double *)MALLOC(scilabSparse.nel*sizeof(double));
-             if (scilabSparse.R==NULL)
-               {
-                 Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
-
-                 FREE(scilabSparse.mnel);
-                 FREE(scilabSparse.icol);
-                 FREE(scilabSparse.R);
-
-                 return FALSE;
-               }
-             scilabSparse.I    = (double *)MALLOC(scilabSparse.nel*sizeof(double));
-             if (scilabSparse.I==NULL)
-               {
-                 Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
-
-                 FREE(scilabSparse.mnel);
-                 FREE(scilabSparse.icol);
-                 FREE(scilabSparse.R);
-                 FREE(scilabSparse.I);
-
-                 return FALSE;
-               }
-             _SciErr = getComplexSparseMatrixInList(pvApiCtx, parent, item_position, &dims[1], &dims[0], 
-                                                    &scilabSparse.nel, &scilabSparse.mnel, 
-                                                    &scilabSparse.icol, &scilabSparse.R, &scilabSparse.I);
-           }
-
-         scilabSparse.it = 1;
-                                                   
-       }
-      else
-       {
-         if (parent==NULL)
-           {
-             getAllocatedSparseMatrix(pvApiCtx, var_addr, &dims[1], &dims[0], 
-                                      &scilabSparse.nel, &scilabSparse.mnel, 
-                                      &scilabSparse.icol, &scilabSparse.R);
-           }
-         else
-           {
-             _SciErr = getSparseMatrixInList(pvApiCtx, parent, item_position, &dims[1], &dims[0], 
-                                             &scilabSparse.nel, NULL, NULL, NULL);
-             scilabSparse.mnel = (int *)MALLOC(dims[1]*sizeof(int));
-             if (scilabSparse.mnel==NULL)
-               {
-                 Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
-
-                 FREE(scilabSparse.mnel);
-
-                 return FALSE;
-               }
-             scilabSparse.icol = (int *)MALLOC(scilabSparse.nel*sizeof(int));
-             if (scilabSparse.icol==NULL)
-               {
-                 Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
-
-                 FREE(scilabSparse.mnel);
-                 FREE(scilabSparse.icol);
-
-                 return FALSE;
-               }
-             scilabSparse.R    = (double *)MALLOC(scilabSparse.nel*sizeof(double));
-             if (scilabSparse.R==NULL)
-               {
-                 Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
-
-                 FREE(scilabSparse.mnel);
-                 FREE(scilabSparse.icol);
-                 FREE(scilabSparse.R);
-
-                 return FALSE;
-               }
-             _SciErr = getSparseMatrixInList(pvApiCtx, parent, item_position, &dims[1], &dims[0], 
-                                             &scilabSparse.nel, &scilabSparse.mnel, 
-                                             &scilabSparse.icol, &scilabSparse.R);
-           }
-
-         scilabSparse.it = 0;
-       }
-
-      scilabSparse.m = dims[1];
-      scilabSparse.n = dims[0];
-
-      /* colIndexes = (int*) MALLOC(sizeof(int) *  (scilabSparse.nel + 1));  */
-      colIndexes = (int*) MALLOC(sizeof(int) *  (scilabSparse.m + 1)); 
-      if (colIndexes==NULL)
+        scilabSparse.m = dims[1];
+        scilabSparse.n = dims[0];
+
+        /* colIndexes = (int*) MALLOC(sizeof(int) *  (scilabSparse.nel + 1));  */
+        colIndexes = (int*) MALLOC(sizeof(int) *  (scilabSparse.m + 1));
+        if (colIndexes == NULL)
         {
-          Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
-
-         if (scilabSparse.it)
-           {
-             freeAllocatedComplexSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R, scilabSparse.I);
-           }
-         else
-           {
-             freeAllocatedSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R);
-           }
-
-          return FALSE;
+            Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
+
+            if (scilabSparse.it)
+            {
+                freeAllocatedComplexSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R, scilabSparse.I);
+            }
+            else
+            {
+                freeAllocatedSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R);
+            }
+
+            return FALSE;
         }
-      
-      colIndexes[0] = 0;
-      /* for (K=0; K<scilabSparse.nel; K++) */
-      for (K=0; K<scilabSparse.m; K++)
+
+        colIndexes[0] = 0;
+        /* for (K=0; K<scilabSparse.nel; K++) */
+        for (K = 0; K < scilabSparse.m; K++)
         {
-          colIndexes[K+1] = colIndexes[K] + scilabSparse.mnel[K];
+            colIndexes[K + 1] = colIndexes[K] + scilabSparse.mnel[K];
         }
-      rowIndexes = (int*) MALLOC(sizeof(int) *  scilabSparse.nel);
-      if (rowIndexes==NULL)
+
+        rowIndexes = (int*) MALLOC(sizeof(int) *  scilabSparse.nel);
+        if (rowIndexes == NULL)
         {
-          Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
-
-         if (scilabSparse.it)
-           {
-             freeAllocatedComplexSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R, scilabSparse.I);
-           }
-         else
-           {
-             freeAllocatedSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R);
-           }
-
-          return FALSE;
+            Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
+            free(colIndexes);
+
+            if (scilabSparse.it)
+            {
+                freeAllocatedComplexSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R, scilabSparse.I);
+            }
+            else
+            {
+                freeAllocatedSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R);
+            }
+
+            return FALSE;
         }
-      
-      for (K=0; K<scilabSparse.nel; K++)
+
+        for (K = 0; K < scilabSparse.nel; K++)
         {
-          rowIndexes[K] = scilabSparse.icol[K] - 1;
+            rowIndexes[K] = scilabSparse.icol[K] - 1;
         }
 
-      if (scilabSparse.it==0) /* Real sparse */
+        if (scilabSparse.it == 0) /* Real sparse */
         {
-          if ((data = (double*) MALLOC(sizeof(double) * scilabSparse.nel)) == NULL)
+            if ((data = (double*) MALLOC(sizeof(double) * scilabSparse.nel)) == NULL)
+            {
+                Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
+                free(rowIndexes);
+                free(colIndexes);
+                if (scilabSparse.it)
+                {
+                    freeAllocatedComplexSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R, scilabSparse.I);
+                }
+                else
+                {
+                    freeAllocatedSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R);
+                }
+
+                return 0;
+            }
+
+            for (K = 0; K < scilabSparse.nel; K++)
             {
-              Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
-
-             if (scilabSparse.it)
-               {
-                 freeAllocatedComplexSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R, scilabSparse.I);
-               }
-             else
-               {
-                 freeAllocatedSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R);
-               }
-             
-             return 0;
-           }
-
-          for(K = 0; K < scilabSparse.nel; K++) 
-             { 
-               data[K] = scilabSparse.R[K]; 
-             } 
+                data[K] = scilabSparse.R[K];
+            }
         }
-      else
+        else
         {
-          if ((data = (double*) MALLOC(2 * sizeof(double) * scilabSparse.nel)) == NULL)
+            if ((data = (double*) MALLOC(2 * sizeof(double) * scilabSparse.nel)) == NULL)
             {
-              Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
-
-             if (scilabSparse.it)
-               {
-                 freeAllocatedComplexSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R, scilabSparse.I);
-               }
-             else
-               {
-                 freeAllocatedSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R);
-               }
-             
-              return FALSE;
+                Scierror(999, _("%s: No more memory.\n"), "GetSparseVariable");
+
+                if (scilabSparse.it)
+                {
+                    freeAllocatedComplexSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R, scilabSparse.I);
+                }
+                else
+                {
+                    freeAllocatedSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R);
+                }
+                free(rowIndexes);
+                free(colIndexes);
+                return FALSE;
             }
 
-          for(K = 0; K < scilabSparse.nel; K++)
+            for (K = 0; K < scilabSparse.nel; K++)
             {
-              data[K] = scilabSparse.R[K];
+                data[K] = scilabSparse.R[K];
             }
 
-          for(K = 0; K < scilabSparse.nel; K++)
+            for (K = 0; K < scilabSparse.nel; K++)
             {
-              data[K + scilabSparse.nel] = scilabSparse.I[K];
+                data[K + scilabSparse.nel] = scilabSparse.I[K];
             }
         }
 
-      /* Create Matlab Sparse matrix data */
-      sparseData->nzmax = scilabSparse.nel;
-      sparseData->nir   = scilabSparse.nel;
-      sparseData->ir    = rowIndexes;
-      /* sparseData->njc   = scilabSparse.nel + 1; */
-      sparseData->njc   = scilabSparse.m + 1;
-      sparseData->jc    = colIndexes;
-      sparseData->ndata = scilabSparse.nel;
-      sparseData->data  = (void*) data;
-
-      if (scilabSparse.it == 0)
+        /* Create Matlab Sparse matrix data */
+        sparseData->nzmax = scilabSparse.nel;
+        sparseData->nir   = scilabSparse.nel;
+        sparseData->ir    = rowIndexes;
+        /* sparseData->njc   = scilabSparse.nel + 1; */
+        sparseData->njc   = scilabSparse.m + 1;
+        sparseData->jc    = colIndexes;
+        sparseData->ndata = scilabSparse.nel;
+        sparseData->data  = (void*) data;
+
+        if (scilabSparse.it == 0)
         {
-          createdVar = Mat_VarCreate(name, MAT_C_SPARSE, MAT_T_DOUBLE, rank, dims, sparseData, 0 | MEM_CONSERVE);
+            createdVar = Mat_VarCreate(name, MAT_C_SPARSE, MAT_T_DOUBLE, rank, dims, sparseData, 0 | MEM_CONSERVE);
         }
-      else
+        else
         {
-          createdVar = Mat_VarCreate(name, MAT_C_SPARSE, MAT_T_DOUBLE, rank, dims, sparseData, MAT_F_COMPLEX | MEM_CONSERVE);
-         if (data) FREE(data);
+            createdVar = Mat_VarCreate(name, MAT_C_SPARSE, MAT_T_DOUBLE, rank, dims, sparseData, MAT_F_COMPLEX | MEM_CONSERVE);
+            if (data)
+            {
+                FREE(data);
+            }
         }
 
-      if (dims) FREE(dims);
+        if (dims)
+        {
+            FREE(dims);
+        }
     }
-  else
+    else
     {
-      Scierror(999, _("%s: Wrong type for first input argument: Sparse matrix expected.\n"), "GetSparseVariable");
+        Scierror(999, _("%s: Wrong type for first input argument: Sparse matrix expected.\n"), "GetSparseVariable");
     }
-  
-  if (scilabSparse.it)
+
+    if (scilabSparse.it)
     {
-      freeAllocatedComplexSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R, scilabSparse.I);
+        freeAllocatedComplexSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R, scilabSparse.I);
     }
-  else
+    else
     {
-      freeAllocatedSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R);
+        freeAllocatedSparseMatrix(scilabSparse.mnel, scilabSparse.icol, scilabSparse.R);
     }
-  
-  return createdVar;
+
+    return createdVar;
 }
index 588c4d7..ab98316 100644 (file)
@@ -162,6 +162,13 @@ void UpdateBrowseVar(BOOL update)
 
     freeArrayOfString(pstAllVariableNames, iLocalVariablesUsed + iGlobalVariablesUsed);
     freeArrayOfString(pstAllVariableVisibility, iLocalVariablesUsed + iGlobalVariablesUsed);
+
+    if (piAllVariableFromUser)
+    {
+        FREE(piAllVariableFromUser);
+        piAllVariableFromUser = NULL;
+    }
+
     if (piAllVariableBytes)
     {
         FREE(piAllVariableBytes);