C extension using GSL

Nick Craig-Wood nick at craig-wood.com
Sat Mar 28 06:30:04 EDT 2009


Gabriel Genellina <gagsl-py2 at yahoo.com.ar> wrote:
>  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  
>  object:
> 
>  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)
>  3
>  py> x = ()
>  py> rc(x)
>  954   # the empty tuple is shared

That reminds me, you can use the gc module to show all your objects
that are in use, which can help with memory leaks.

eg something like

http://code.activestate.com/recipes/457665/

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list