* Bug #12559 - Fixed some memory leaks in FFTW 08/13808/10
Paul Bignier [Mon, 24 Feb 2014 10:02:33 +0000 (11:02 +0100)]
Slightly improve the situation but does not fix all memleaks.

Change-Id: I20261b0e117b64ba4080ba76f2605f9e9f470a92

scilab/CHANGES
scilab/modules/fftw/sci_gateway/c/sci_fftw_flags.c
scilab/modules/fftw/sci_gateway/c/sci_set_fftw_wisdom.c
scilab/modules/fftw/sci_gateway/cpp/sci_dct.cpp
scilab/modules/fftw/src/c/fftw_utilities.c
scilab/modules/fftw/src/cpp/fftw_common.cpp
scilab/modules/fftw/tests/nonreg_tests/bug_12559.tst [new file with mode: 0644]

index 7435cfd..c30d0bd 100644 (file)
@@ -319,7 +319,9 @@ In 6.0.0:
 
 * Bug #9456 fixed  - bench_run did not work on a path or in a toolbox
 
-* Bug #12872 fixed - help pages of else, elseif, end, try, sciargs, global, halt, empty and power were in wrong help sections
+* Bug #12559       - Fixed some memory leaks in FFTW
+
+* Bug #12872       - help pages of else, elseif, end, try, sciargs, global, halt, empty and power were in wrong help sections
 
 * Bug #13465 fixed - The display of polyline .display_function and .display_function properties was not conventional
 
index c1e70b6..4ea38bd 100644 (file)
@@ -209,6 +209,7 @@ int sci_fftw_flags(char *fname,  void* pvApiCtx)
                 sciErr = getMatrixOfString(pvApiCtx, piAddr1, &m1, &n1, piLen, NULL);
                 if (sciErr.iErr)
                 {
+                    free(piLen);
                     printError(&sciErr, 0);
                     return 1;
                 }
@@ -223,6 +224,8 @@ int sci_fftw_flags(char *fname,  void* pvApiCtx)
                 sciErr = getMatrixOfString(pvApiCtx, piAddr1, &m1, &n1, piLen, Str1);
                 if (sciErr.iErr)
                 {
+                    free(piLen);
+                    freeArrayOfString(Str1, m1 * n1);
                     printError(&sciErr, 0);
                     return 1;
                 }
@@ -239,6 +242,7 @@ int sci_fftw_flags(char *fname,  void* pvApiCtx)
 
                     if (i == nb_flag)
                     {
+                        free(piLen);
                         freeArrayOfString(Str1, m1 * n1);
                         Scierror(999, _("%s: Wrong values for input argument #%d: FFTW flag expected.\n"), fname, 1);
                         return 0;
@@ -253,6 +257,7 @@ int sci_fftw_flags(char *fname,  void* pvApiCtx)
                 }
 
                 uiVar1 = (unsigned int)flagv;
+                free(piLen);
                 freeArrayOfString(Str1, m1 * n1);
                 m1 = 1;
                 n1 = 1;
index 49a1861..553df39 100644 (file)
@@ -84,6 +84,7 @@ int sci_set_fftw_wisdom(char *fname, void* pvApiCtx)
     sciErr = getMatrixOfString(pvApiCtx, piAddr1, &m1, &n1, piLen, NULL);
     if (sciErr.iErr)
     {
+        free(piLen);
         printError(&sciErr, 0);
         return 1;
     }
@@ -98,6 +99,9 @@ int sci_set_fftw_wisdom(char *fname, void* pvApiCtx)
     sciErr = getMatrixOfString(pvApiCtx, piAddr1, &m1, &n1, piLen, Str1);
     if (sciErr.iErr)
     {
+        free(piLen);
+        freeArrayOfString(Str1, m1 * n1);
+        free(Str1);
         printError(&sciErr, 0);
         return 1;
     }
@@ -131,6 +135,7 @@ int sci_set_fftw_wisdom(char *fname, void* pvApiCtx)
     }
     Str[k - 1] = '\0';
 
+    free(piLen);
     freeArrayOfString(Str1, m1 * n1);
 
     if (!(call_fftw_import_wisdom_from_string(Str)))
index 7605435..25afa74 100644 (file)
@@ -125,11 +125,13 @@ int sci_dct_gen(const char *fname, types::Double* A, types::Double** O, int isn,
         if (errflag == 1)
         {
             Scierror(999, _("%s: Cannot allocate more memory.\n"), fname);
+            FREE(kind);
             return types::Function::Error;
         }
         else if (errflag == 2)
         {
             Scierror(999, _("%s: Creation of requested fftw plan failed.\n"), fname);
+            FREE(kind);
             return types::Function::Error;
         }
         /* execute FFTW plan */
@@ -158,15 +160,13 @@ int sci_dct_gen(const char *fname, types::Double* A, types::Double** O, int isn,
         if (errflag == 1)
         {
             Scierror(999, _("%s: Cannot allocate more memory.\n"), fname);
-            FREE(dims1);
-            FREE(incr1);
+            FREE(kind);
             return types::Function::Error;
         }
         else if (errflag == 2)
         {
             Scierror(999, _("%s: Creation of requested fftw plan failed.\n"), fname);
-            FREE(dims1);
-            FREE(incr1);
+            FREE(kind);
             return types::Function::Error;
         }
 
@@ -175,8 +175,7 @@ int sci_dct_gen(const char *fname, types::Double* A, types::Double** O, int isn,
         if ((dims1 = (int *)MALLOC(sizeof(int) * howmany_rank)) == NULL)
         {
             Scierror(999, _("%s: Cannot allocate more memory.\n"), fname);
-            FREE(dims1);
-            FREE(incr1);
+            FREE(kind);
             return types::Function::Error;
         }
         dims1[0] = howmany_dims[0].n;
@@ -189,8 +188,8 @@ int sci_dct_gen(const char *fname, types::Double* A, types::Double** O, int isn,
         if ((incr1 = (int *)MALLOC(sizeof(int) * howmany_rank)) == NULL)
         {
             Scierror(999, _("%s: Cannot allocate more memory.\n"), fname);
+            FREE(kind);
             FREE(dims1);
-            FREE(incr1);
             return types::Function::Error;
         }
         t = 1;
@@ -239,6 +238,7 @@ int sci_dct_gen(const char *fname, types::Double* A, types::Double** O, int isn,
         if (dct_scale_array(Ar, Ai, gdim, isn) == -1)
         {
             Scierror(999, _("%s: Cannot allocate more memory.\n"), fname);
+            FREE(kind);
             return types::Function::Error;
         }
     }
index a2344d3..6f05668 100644 (file)
@@ -170,6 +170,7 @@ fftw_plan GetFFTWPlan(enum Plan_Type type, guru_dim_struct *gdim,
         {
             if ((Sci_Plan->kind = (fftw_r2r_kind *) MALLOC(sizeof(fftw_r2r_kind) * (gdim->rank))) == NULL)
             {
+                FREE(Sci_Plan->gdim.dims);
                 *errflag = 1;
                 return (NULL);
             }
@@ -753,6 +754,8 @@ int check_array_symmetry(double *Ar, double *Ai, guru_dim_struct gdim)
                         {
                             if ((r = check_1D_symmetry(Ar + j, NULL, gdim.dims[0].n, gdim.dims[0].is)) != 1 )
                             {
+                                FREE(dims1);
+                                FREE(incr1);
                                 return r;
                             }
                             j += gdim.howmany_dims[0].is;
@@ -776,6 +779,8 @@ int check_array_symmetry(double *Ar, double *Ai, guru_dim_struct gdim)
                         {
                             if ((r = check_1D_symmetry(Ar + j, Ai + j, gdim.dims[0].n, gdim.dims[0].is)) != 1 )
                             {
+                                FREE(dims1);
+                                FREE(incr1);
                                 return r;
                             }
                             j += gdim.howmany_dims[0].is;
@@ -791,6 +796,7 @@ int check_array_symmetry(double *Ar, double *Ai, guru_dim_struct gdim)
                     }
                 }
                 FREE(dims1);
+                FREE(incr1);
                 return 1;
             case 2:  /* multiple 2D fft */
                 if (Ai == NULL)
@@ -803,6 +809,8 @@ int check_array_symmetry(double *Ar, double *Ai, guru_dim_struct gdim)
                             if ((r = check_2D_symmetry(Ar + j, NULL, gdim.dims[0].n, gdim.dims[0].is,
                                                        gdim.dims[1].n, gdim.dims[1].is)) != 1 )
                             {
+                                FREE(dims1);
+                                FREE(incr1);
                                 return r;
                             }
                             j += gdim.howmany_dims[0].is;
@@ -828,6 +836,8 @@ int check_array_symmetry(double *Ar, double *Ai, guru_dim_struct gdim)
                             if ((r = check_2D_symmetry(Ar + j, Ai + j, gdim.dims[0].n, gdim.dims[0].is,
                                                        gdim.dims[1].n, gdim.dims[1].is)) != 1 )
                             {
+                                FREE(dims1);
+                                FREE(incr1);
                                 return r;
                             }
                             j += gdim.howmany_dims[0].is;
@@ -848,11 +858,15 @@ int check_array_symmetry(double *Ar, double *Ai, guru_dim_struct gdim)
             default: /*general N-D case*/
                 if ((dims = (int *)MALLOC(sizeof(int) * gdim.rank)) == NULL)
                 {
+                    FREE(dims1);
+                    FREE(incr1);
                     return -1;
                 }
                 if ((incr = (int *)MALLOC(sizeof(int) * gdim.rank)) == NULL)
                 {
                     FREE(dims);
+                    FREE(dims1);
+                    FREE(incr1);
                     return -1;
                 }
                 for (i = 0; i < ndims; i++)
@@ -876,8 +890,9 @@ int check_array_symmetry(double *Ar, double *Ai, guru_dim_struct gdim)
                         if (r <= 0)
                         {
                             FREE(dims);
-                            FREE(incr);
                             FREE(dims1);
+                            FREE(incr);
+                            FREE(incr1);
                             return r;
                         }
                         j += gdim.howmany_dims[0].is;
@@ -892,8 +907,9 @@ int check_array_symmetry(double *Ar, double *Ai, guru_dim_struct gdim)
                     }
                 }
                 FREE(dims);
-                FREE(incr);
                 FREE(dims1);
+                FREE(incr);
+                FREE(incr1);
                 return 1;
         }
     }
@@ -1234,6 +1250,7 @@ int complete_array(double *Ar, double *Ai, guru_dim_struct gdim)
                     }
                 }
                 FREE(dims1);
+                FREE(incr1);
                 return 0;
             case 2: /* multiple 2D fft */
                 if (Ai == NULL)
@@ -1272,16 +1289,20 @@ int complete_array(double *Ar, double *Ai, guru_dim_struct gdim)
                     }
                 }
                 FREE(dims1);
+                FREE(incr1);
                 return 0;
             default:  /* multiple ND fft */
                 if ((dims = (int *)MALLOC(sizeof(int) * gdim.rank)) == NULL)
                 {
+                    FREE(dims1);
+                    FREE(incr1);
                     return -1;
                 }
                 if ((incr = (int *)MALLOC(sizeof(int) * gdim.rank)) == NULL)
                 {
                     FREE(dims);
                     FREE(dims1);
+                    FREE(incr1);
                     return -1;
                 }
                 for (i = 0; i < ndims; i++)
@@ -1303,8 +1324,9 @@ int complete_array(double *Ar, double *Ai, guru_dim_struct gdim)
                     if (r < 0)
                     {
                         FREE(dims);
-                        FREE(incr);
                         FREE(dims1);
+                        FREE(incr);
+                        FREE(incr1);
                         return r;
                     }
                     j += gdim.howmany_dims[0].is;
@@ -1317,10 +1339,9 @@ int complete_array(double *Ar, double *Ai, guru_dim_struct gdim)
                         }
                     }
                 }
-
                 FREE(dims);
-                FREE(incr);
                 FREE(dims1);
+                FREE(incr);
                 FREE(incr1);
         }
     }
index 4288faf..f687547 100644 (file)
@@ -256,7 +256,7 @@ types::Function::ReturnValue common_2args(std::string& name, types::typed_list &
         Scierror(999, _("%s: Cannot allocate more memory.\n"), name.data());
         FREE(gdim.dims);
         FREE(gdim.howmany_dims);
-        return types::Function::OK;
+        return types::Function::Error;
     }
 
     j = 0;
@@ -282,7 +282,7 @@ types::Function::ReturnValue common_2args(std::string& name, types::typed_list &
     {
         FREE(gdim.dims);
         FREE(gdim.howmany_dims);
-        return types::Function::OK;
+        return types::Function::Error;
     }
     FREE(gdim.dims);
     FREE(gdim.howmany_dims);
@@ -524,6 +524,9 @@ types::Function::ReturnValue common_3args(std::string& name, types::typed_list &
         return types::Function::Error;
     }
 
+    FREE(gdim.dims);
+    FREE(gdim.howmany_dims);
+    FREE(Sel);
     out.push_back(D);
     return types::Function::OK;
 }
diff --git a/scilab/modules/fftw/tests/nonreg_tests/bug_12559.tst b/scilab/modules/fftw/tests/nonreg_tests/bug_12559.tst
new file mode 100644 (file)
index 0000000..828e1c1
--- /dev/null
@@ -0,0 +1,25 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2016 - Scilab Enterprises - Paul Bignier
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- CLI SHELL MODE -->
+//
+// <-- LONG TIME EXECUTION -->
+//
+// <-- Non-regression test for bug 12559 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/show_bug.cgi?id=12559
+//
+// <-- Short Description -->
+// Some variables were not freed and generated important memory leaks.
+
+U = rand(32, 32);
+Nit = 10000000;
+
+for it=1:Nit
+    U = dct(U);
+end