C extension using GSL

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Mar 27 21:54:25 CET 2009

En Fri, 27 Mar 2009 03:10:06 -0300, jesse <jberwald at gmail.com> escribió:

> I give up. I cannot find my memory leak! I'm hoping that someone out
> there has come across something similar. Let me lay out the basic
> setup:
> [...]
> 4) C: A PyList object, L,  is created (new reference!). This will hold
> the solution vector for the ODE
> [...]
> 7) C: Return L to Python with return Py_BuildValue("N", L).

I don't know the "N" format, but if Py_BuildValue returns a new reference,  
you're leaking a reference to the L list.
Why don't you return L directly?

You can use sys.getrefcount in Python to see the reference count for an  

py> from sys import getrefcount as rc
py> x = object()
py> rc(x)
2   # the name x, and a temporary reference as parameter
py> rc([])
1   # only the temporary reference
py> x = y = []
py> rc(x)
py> x = ()
py> rc(x)
954   # the empty tuple is shared

Gabriel Genellina

More information about the Python-list mailing list