shithub: sox

Download patch

ref: ed9b202a7e6f5460c2ef537881eb29dac862a8a5
parent: c443ca782b15056814dea30c8574487366c3c164
author: Ulrich Klauer <ulrich@chirlu.de>
date: Sat Sep 1 14:28:56 EDT 2012

spectrogram: fix memory leak

Free the (rather large) memory blocks used to store the per-channel
spectrogram data for all channels. Previously, only those of the first
channel were freed upon stopping the effect.

--- a/ChangeLog
+++ b/ChangeLog
@@ -35,6 +35,7 @@
 
   o Speed optimization for effects that operate on channels
     independently. (Ulrich Klauer)
+  o Fix memory leaks. (Ulrich Klauer)
 
 
 sox-14.4.1	20xx-xx-xx
--- a/src/spectrogram.c
+++ b/src/spectrogram.c
@@ -272,6 +272,7 @@
   }
   ++p->cols;
   p->dBfs = lsx_realloc(p->dBfs, p->cols * p->rows * sizeof(*p->dBfs));
+    /* FIXME: allocate in larger steps (for several columns) */
   for (i = 0; i < p->rows; ++i) {
     double dBfs = 10 * log10(p->magnitudes[i] * p->block_norm);
     p->dBfs[(p->cols - 1) * p->rows + i] = dBfs + p->gain;
@@ -498,7 +499,7 @@
 #define spectrum_width 14
 #define right 35
 
-static int stop(sox_effect_t * effp)
+static int stop(sox_effect_t * effp) /* only called, by end(), on flow 0 */
 {
   priv_t *    p        = (priv_t *) effp->priv;
   FILE *      file;
@@ -624,7 +625,14 @@
   return SOX_SUCCESS;
 }
 
-static int end(sox_effect_t * effp) {return effp->flow? SOX_SUCCESS:stop(effp);}
+static int end(sox_effect_t * effp)
+{
+  priv_t *p = (priv_t *)effp->priv;
+  if (effp->flow == 0)
+    return stop(effp);
+  free(p->dBfs);
+  return SOX_SUCCESS;
+}
 
 sox_effect_handler_t const * lsx_spectrogram_effect_fn(void)
 {