shithub: aubio

Download patch

ref: 202697ace5750f91e7b71878c482b2e4788f69d2
parent: 59cb451fc91171a071fbf2837021a8f00efaa8b4
author: Paul Brossier <piem@piem.org>
date: Mon Apr 18 16:32:07 EDT 2016

ext/py-phasevoc.c: continue fixing memory leak (#49)

--- a/python/ext/py-phasevoc.c
+++ b/python/ext/py-phasevoc.c
@@ -2,8 +2,17 @@
 
 static char Py_pvoc_doc[] = "pvoc object";
 
-AUBIO_DECLARE(pvoc, uint_t win_s; uint_t hop_s)
+typedef struct
+{
+  PyObject_HEAD
+  aubio_pvoc_t * o;
+  uint_t win_s;
+  uint_t hop_s;
+  cvec_t *output;
+  fvec_t *routput;
+} Py_pvoc;
 
+
 //AUBIO_NEW(pvoc)
 static PyObject *
 Py_pvoc_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
@@ -46,6 +55,9 @@
     return NULL;
   }
 
+  self->output = new_cvec(self->win_s);
+  self->routput = new_fvec(self->hop_s);
+
   return (PyObject *) self;
 }
 
@@ -52,14 +64,21 @@
 
 AUBIO_INIT(pvoc, self->win_s, self->hop_s)
 
-AUBIO_DEL(pvoc)
+static void
+Py_pvoc_del (Py_pvoc *self, PyObject *unused)
+{
+  del_aubio_pvoc(self->o);
+  del_cvec(self->output);
+  del_fvec(self->routput);
+  self->ob_type->tp_free((PyObject *) self);
+}
 
+
 static PyObject * 
 Py_pvoc_do(Py_pvoc * self, PyObject * args)
 {
   PyObject *input;
   fvec_t *vec;
-  cvec_t *output;
 
   if (!PyArg_ParseTuple (args, "O", &input)) {
     return NULL;
@@ -71,11 +90,9 @@
     return NULL;
   }
 
-  output = new_cvec(self->win_s);
-
   // compute the function
-  aubio_pvoc_do (self->o, vec, output);
-  return (PyObject *)PyAubio_CCvecToPyCvec(output);
+  aubio_pvoc_do (self->o, vec, self->output);
+  return (PyObject *)PyAubio_CCvecToPyCvec(self->output);
 }
 
 AUBIO_MEMBERS_START(pvoc) 
@@ -90,8 +107,6 @@
 {
   PyObject *input;
   cvec_t *vec;
-  fvec_t *output;
-
   if (!PyArg_ParseTuple (args, "O", &input)) {
     return NULL;
   }
@@ -102,11 +117,9 @@
     return NULL;
   }
 
-  output = new_fvec(self->hop_s);
-
   // compute the function
-  aubio_pvoc_rdo (self->o, vec, output);
-  return (PyObject *)PyAubio_CFvecToArray(output);
+  aubio_pvoc_rdo (self->o, vec, self->routput);
+  return (PyObject *)PyAubio_CFvecToArray(self->routput);
 }
 
 static PyMethodDef Py_pvoc_methods[] = {