[MATRIX-SIG] memory leak

Conrad Huang %CGL conrad@cgl.ucsf.edu
20 Nov 97 16:30:35 GMT


da@skivs.ski.org (David Ascher) writes:

>Is there a known memory leak in NumPy, with a patch available, or did I
>just find a new one?  I vaguely remember something being talked about
>months ago, but clearly it hasn't made it in the latest available
>distribution.

I have a guess as to what the problem may be, and I was going to wait
until 1.5b1 came out to see if it's already fixed.  But now that you
asked...

The problem I think I discovered is not in NumPy, but it manifested
itself while I was working with a program that made extensive use of
LinearAlgebra.eigenvectors(), which calls lapack_lite.dgeev().  The
latter routine returns a dictionary of a bunch of things and constructs
the dictionary with a call to:

	return Py_BuildValue("{s:i,s:c,s:c,s:i,s:i,s:i,s:i,s:i,s:i}",
	"dgeev_",lapack_lite_status__,"jobvl",jobvl,"jobvr",jobvr,
	"n",n,"lda",lda,"ldvl",ldvl,"ldvr",ldvr,"lwork",lwork,"info",info);

When Py_BuildValue (in Python/modsupport.c) builds the dictionary, it
calls do_mkvalue() which calls do_mkdict(), and that's where I think
the problem is.  do_mkdict() goes through a loop whose body is (with
error checking removed):

	k = do_mkvalue(p_format, p_va);
	v = do_mkvalue(p_format, p_va);
	PyDict_SetItem(d, k, v);

Since PyDict_SetItem() increments the reference count of both key and
value, k and v both have reference counts of 2 when do_mkdict() returns.
I think there should be:

	Py_DECREF(k);
	Py_DECREF(v);

after the call to PyDict_SetItem().

Will someone "in the know" please check my logic?  Thanks,

Conrad

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________