[Numpy-discussion] memory not being freed

Reggie Dugard reggie at merfinllc.com
Fri Feb 22 17:26:06 CST 2002


I believe PyArray_FromDims() returns a new reference to the object (arr) 
and that the Py_BuildValue creates another reference so that you've got 
two references to that array and python is only going to DECREF one when 
it's done.  I would suggest either 1) using the 'N' format character to 
Py_BuildValue so that another reference isn't created or 2) explicitly 
calling Py_DECREF(arr) just before you return.

Reggie

>>>>>>>>>>>>>>>>>> Original Message <<<<<<<<<<<<<<<<<<

On 2/22/02, 4:19:15 PM, Mathew Yeates <mathew at fugue.jpl.nasa.gov> wrote 
regarding [Numpy-discussion] memory not being freed:


> Hi
> I'm having problems with garbage collection
> I wrote an extension which creates an array
> and returns it

> foo() {
> arr = (PyArrayObject *) PyArray_FromDims( .....
> ret = Py_BuildValue("O", arr);
> return ret;
> }


> but now if I do
> while 1:
>       a=foo()



> memory is never free'd. I've even tried explicitly calling gc.collect and
> adding del(a) after a=foo.

> Is the problem that Py_BuildValue increases the reference count?

> Mathew


> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion




More information about the Numpy-discussion mailing list