[Numpy-discussion] Solving a memory leak in a numpy extension; PyArray_ContiguousFromObject

Lisandro Dalcin dalcinl at gmail.com
Fri Apr 17 20:36:38 EDT 2009


1) Calling both PyArray_XDECREF(array) and Py_DECREF(array) is likely wrong.
2) Py_DECREF(input) should never be done.

On Fri, Apr 17, 2009 at 12:25 PM, Dan S <dan.s.towell+numpy at gmail.com> wrote:
> Hi -
>
> I have written a numpy extension which works fine but has a memory
> leak. It takes a single array argument and returns a single scalar.
> After reducing the code down in order to chase the problem, I have the
> following:
>
> static PyObject * kdpee_pycall(PyObject *self, PyObject *args)
> {
>        PyObject *input;
>        PyArrayObject *array;
>        int n, numdims;
>
>        if (!PyArg_ParseTuple(args, "O", &input))
>                return NULL;
>
>        // Ensure we have contiguous, 2D, floating-point data:
>        array = (PyArrayObject*) PyArray_ContiguousFromObject(input,
> PyArray_DOUBLE, 2, 2);
>
>        if(array==NULL){
>                printf("kdpee_py: nullness!\n");
>                return NULL;
>        }
>        PyArray_XDECREF(array);
>        Py_DECREF(array); // destroy the contig array
>        Py_DECREF(input); // is this needed? doc says no, but it seems to fix
> a reference-count-leak
>        return PyFloat_FromDouble(3.1415927); // temporary
> }
>
>
> The test code is
>
>      from numpy import *
>      from kdpee import *
>      import sys
>      a = array( [[1,2,3,4,5,1,2,3,4,5], [6,7,8,9,0,6,7,8,9,0]])
>      sys.getrefcount(a)
>      for i in range(5000): tmp = kdpee(a)
>
>      sys.getrefcount(a)
>
> Every time I run this code I get "2" back from both calls to
> sys.getrefcount(a), but I'm still getting a memory leak of about 0.4
> MB. What am I doing wrong?
>
> I'll be grateful for any suggestions you can offer.
>
> Thanks
> Dan
>
> P.S. My system is python 2.5.4 (from macports) on intel Mac 10.4.11,
> numpy provided by macports package "py25-numpy" v1.2.1
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594



More information about the NumPy-Discussion mailing list