[Matrix-SIG] Numpy array.tolist() memory leak ?

David Ascher da@skivs.ski.org
Sun, 27 Sep 1998 23:50:44 -0700 (Pacific Daylight Time)


On Sun, 27 Sep 1998, Jonah Lee wrote:
> 
> There seems to be a memory leak with the following example:
> 
> from Numeric import *
> a=array([[1,2,3],[4,5,6],[7,8,9]], Float)
> for i in range(100000):
>     for j in range(100000):
>         c=a.tolist()
> 
> If a=array([1,2,3,4]), then there is no leak.
> 
> Any advice on this would be appreciated. Thanks.

I could be wrong (reference counting was never my forte), but I believe
that arrayobject.c's PyArray_ToList() should be modified to read:

        ...

	for (i=0; i<sz; i++) {
!           PyArrayObject *elt;
!           elt = array_item((PyArrayObject *)self, i);
!           PyList_SetItem(lp, i, PyArray_ToList(elt));
!           Py_DECREF(elt);
	}

        ...

otherwise the subarrays ([1,2,3] etc. in the example above) never get
DECREF'ed out.

--david ascher