[Numpy-discussion] PATCH: arange leaks references to provided dtype

Travis Oliphant teoliphant@gmail....
Wed Jun 30 17:52:49 CDT 2010


On Jun 30, 2010, at 3:18 PM, Lisandro Dalcin wrote:

> Sorry, no time to make a proper bug report in the tracker.
> 
> This bug have been annoying me from about 3 years, today I finally
> discovered the actual issue!!.

Thanks so much.    This is an excellent fix.    

-Travis





> 
> The code below shows that arange leaks references to dtype (actually,
> PyArray_Descr instances).
> 
> import sys
> import numpy as np
> 
> t = np.dtype('d')
> while 1:
>    a = np.arange(1,dtype=t)
>    print (sys.getrefcount(t))
> 
> 
> I see this in numpy 1.3.0 on py2.6 (Fedora 12). This is also present
> in numpy svn/trunk on py2.7 and 3.1
> 
> 
> The trivial patch below seems to fix this refcount bug:
> 
> Index: numpy/core/src/multiarray/multiarraymodule.c
> ===================================================================
> --- numpy/core/src/multiarray/multiarraymodule.c	(revision 8467)
> +++ numpy/core/src/multiarray/multiarraymodule.c	(working copy)
> @@ -1863,7 +1863,7 @@
> 
> static PyObject *
> array_arange(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kws) {
> -    PyObject *o_start = NULL, *o_stop = NULL, *o_step = NULL;
> +    PyObject *o_start = NULL, *o_stop = NULL, *o_step = NULL, *range;
>     static char *kwd[]= {"start", "stop", "step", "dtype", NULL};
>     PyArray_Descr *typecode = NULL;
> 
> @@ -1873,7 +1873,9 @@
>         Py_XDECREF(typecode);
>         return NULL;
>     }
> -    return PyArray_ArangeObj(o_start, o_stop, o_step, typecode);
> +    range = PyArray_ArangeObj(o_start, o_stop, o_step, typecode);
> +    Py_XDECREF(typecode);
> +    return range;
> }
> 
> /*NUMPY_API
> 
> 
> 
> 
> -- 
> Lisandro Dalcin
> ---------------
> CIMEC (INTEC/CONICET-UNL)
> Predio CONICET-Santa Fe
> Colectora RN 168 Km 472, Paraje El Pozo
> Tel: +54-342-4511594 (ext 1011)
> Tel/Fax: +54-342-4511169
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

---
Travis Oliphant
Enthought, Inc.
oliphant@enthought.com
1-512-536-1057
http://www.enthought.com





More information about the NumPy-Discussion mailing list