gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 7f38a11: Memory leaks in sigma-clipping and FI


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 7f38a11: Memory leaks in sigma-clipping and FITS reading fixed
Date: Thu, 22 Mar 2018 12:12:05 -0400 (EDT)

branch: master
commit 7f38a111c45df515e219ae637bad7de0fd16396d
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Memory leaks in sigma-clipping and FITS reading fixed
    
    While testing Arithmetic's new `filter-sigclip-median' operator, I noticed
    that the memory consumption just kept on rising and didn't stop until the
    end. Looking closely into the sigma-clipping code, I noticed that we
    weren't freeing the arrays that keep the median, mean and STD values after
    finishing the sigma-clipping iterations. This wasn't noticed until now
    because sigma clipping was usually done once on the whole dataset. However,
    with the filter operation, it is done on every pixel and the few bytes can
    become huge.
    
    While inspecting memory leaks, I also found a few other minor cases and
    corrected them.
    
    I also noticed that the function name of Arithmetic was still the old
    version (`imgarith'). This is now corrected.
---
 bin/arithmetic/arithmetic.c | 5 +++--
 bin/arithmetic/arithmetic.h | 2 +-
 bin/arithmetic/main.c       | 2 +-
 lib/data.c                  | 2 +-
 lib/fits.c                  | 5 +++--
 lib/statistics.c            | 3 ++-
 6 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/bin/arithmetic/arithmetic.c b/bin/arithmetic/arithmetic.c
index 7b2b067..8e27afe 100644
--- a/bin/arithmetic/arithmetic.c
+++ b/bin/arithmetic/arithmetic.c
@@ -404,6 +404,7 @@ wrapper_for_filter(struct arithmeticparams *p, char *token, 
int operator)
             error(EXIT_FAILURE, 0, "lengths of filter along dimensions "
                   "must be positive. The given length in dimension %zu"
                   "is either zero or negative", ndim-i);
+          gal_data_free(comp);
 
           /* Convert the input into size_t and put it into the array that
              keeps the filter size. */
@@ -482,8 +483,8 @@ wrapper_for_filter(struct arithmeticparams *p, char *token, 
int operator)
   /* Add the output to the top of the stack. */
   operands_add(p, NULL, afp.out);
 
-
   /* Clean up and add the output on top of the stack */
+  gal_data_free(zero);
   gal_data_free(afp.input);
   gal_list_data_free(params_list);
 }
@@ -842,7 +843,7 @@ reversepolish(struct arithmeticparams *p)
 /*************             Top function            *************/
 /***************************************************************/
 void
-imgarith(struct arithmeticparams *p)
+arithmetic(struct arithmeticparams *p)
 {
   /* Parse the arguments */
   reversepolish(p);
diff --git a/bin/arithmetic/arithmetic.h b/bin/arithmetic/arithmetic.h
index a9ab1fa..5b52da8 100644
--- a/bin/arithmetic/arithmetic.h
+++ b/bin/arithmetic/arithmetic.h
@@ -41,6 +41,6 @@ enum arithmetic_prog_operators
 
 
 void
-imgarith(struct arithmeticparams *p);
+arithmetic(struct arithmeticparams *p);
 
 #endif
diff --git a/bin/arithmetic/main.c b/bin/arithmetic/main.c
index fd21c48..eddbc57 100644
--- a/bin/arithmetic/main.c
+++ b/bin/arithmetic/main.c
@@ -46,7 +46,7 @@ main (int argc, char *argv[])
   ui_read_check_inputs_setup(argc, argv, &p);
 
   /* Run MakeProfiles */
-  imgarith(&p);
+  arithmetic(&p);
 
   /* Free any allocated space */
   freeandreport(&p, &t1);
diff --git a/lib/data.c b/lib/data.c
index d3e85e1..b7eb330 100644
--- a/lib/data.c
+++ b/lib/data.c
@@ -450,7 +450,7 @@ gal_data_free_contents(gal_data_t *data)
   if(data->type==GAL_TYPE_STRING && data->array)
     {
       strarr=data->array;
-      for(i=0;i<data->size;++i) free(strarr[i]);
+      for(i=0;i<data->size;++i) if(strarr[i]) free(strarr[i]);
     }
 
   /* Free the array. */
diff --git a/lib/fits.c b/lib/fits.c
index 0cf1010..8ef4c7e 100644
--- a/lib/fits.c
+++ b/lib/fits.c
@@ -966,12 +966,12 @@ gal_fits_key_read_from_ptr(fitsfile *fptr, gal_data_t 
*keysll,
         switch(tmp->type)
           {
           case GAL_TYPE_STRING:
-            errno=0;
             tmp->array=strarray=( tmp->array
                                   ? tmp->array
                                   : gal_data_malloc_array(tmp->type, 1,
                                                           __func__,
                                                           "tmp->array") );
+            errno=0;
             valueptr=strarray[0]=malloc(FLEN_VALUE * sizeof *strarray[0]);
             if(strarray[0]==NULL)
               error(EXIT_FAILURE, errno, "%s: %zu bytes for strarray[0]",
@@ -1030,7 +1030,6 @@ gal_fits_key_read_from_ptr(fitsfile *fptr, gal_data_t 
*keysll,
 
         /* Strings need to be cleaned (CFITSIO puts `'' around them with
            some (possiblly) extra space on the two ends of the string. */
-
       }
 }
 
@@ -1551,6 +1550,8 @@ gal_fits_img_read(char *filename, char *hdu, size_t 
minmapsize)
   img=gal_data_alloc(NULL, type, ndim, dsize, NULL, 0, minmapsize,
                      name, unit, NULL);
   blank=gal_blank_alloc_write(type);
+  if(name) free(name);
+  if(unit) free(unit);
   free(dsize);
 
 
diff --git a/lib/statistics.c b/lib/statistics.c
index 36259ce..ec87181 100644
--- a/lib/statistics.c
+++ b/lib/statistics.c
@@ -1910,7 +1910,7 @@ gal_statistics_sigma_clip(gal_data_t *input, float 
multip, float param,
              it means that we have clipped too much and must stop anyway,
              so we don't need an absolute value on the difference! */
           if( bytolerance && num>0 && ((oldstd - *std) / *std) < param )
-            break;
+            { gal_data_free(meanstd); gal_data_free(median_d); break; }
 
           /* Clip all the elements outside of the desired range: since the
              array is sorted, this means to just change the starting
@@ -1960,6 +1960,7 @@ gal_statistics_sigma_clip(gal_data_t *input, float 
multip, float param,
 
   /* Clean up and return. */
   nbs->array=nbs_array;
+  gal_data_free(median_i);
   if(nbs!=input) gal_data_free(nbs);
   return out;
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]