[Python-Dev] funny leak

Kevin Jacobs jacobs@penguin.theopalgroup.com
Mon, 24 Mar 2003 12:37:18 -0500 (EST)


On Mon, 24 Mar 2003, Michael Hudson wrote:

> Tim Peters <tim.one@comcast.net> writes:
> 
> > OK, there's no leaking memory here, but there is a leaking refcount:  the
> > refcount on the int 0 keeps going up.  The compiler has leaked references to
> > little integers before, but offhand I don't recall the details.
> 
> This seems to be all it takes:

Your patch isn't a 100% fix, since a reference can still be leaked if the
PyDict_SetItem fails.  If nobody beats me to it, I can do a validation pass
through compile.c and see how many I can squash.

-Kevin


> 
> Index: compile.c
> ===================================================================
> RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v
> retrieving revision 2.275
> diff -c -C7 -r2.275 compile.c
> *** compile.c	12 Feb 2003 16:56:51 -0000	2.275
> --- compile.c	24 Mar 2003 16:43:28 -0000
> ***************
> *** 4524,4537 ****
> --- 4564,4578 ----
>   	d = PyDict_New();
>   	for (i = PyList_GET_SIZE(list); --i >= 0; ) {
>   		v = PyInt_FromLong(i);
>   		if (v == NULL) 
>   			goto fail;
>   		if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0)
>   			goto fail;
> + 		Py_DECREF(v);
>   		if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0)
>   			goto fail;
>   	}
>   	pos = 0;
>   	i = PyList_GET_SIZE(list);
>   	Py_DECREF(list);
>   	while (PyDict_Next(*cellvars, &pos, &v, &w)) {
> 
> ... found by the obscure strategy of searching for "PyInt_FromLong" in
> Python/compile.c ...
> 
> A quick eyeballing suggests there are a bunch more of these, but only
> on error returns.
> 
> Cheers,
> M.
> 
> 

-- 
--
Kevin Jacobs
The OPAL Group - Enterprise Systems Architect
Voice: (216) 986-0710 x 19         E-mail: jacobs@theopalgroup.com
Fax:   (216) 986-0714              WWW:    http://www.theopalgroup.com