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

Charles R Harris charlesr.harris@gmail....
Fri Apr 17 20:44:28 CDT 2009


On Fri, Apr 17, 2009 at 7:37 PM, Charles R Harris <charlesr.harris@gmail.com
> wrote:

>
>
> On Fri, Apr 17, 2009 at 9:25 AM, Dan S <dan.s.towell+numpy@gmail.com<dan.s.towell%2Bnumpy@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);
>
>            ^^^^^^^^^^^^^^^^^^^
> Not needed.
>
>>
>>        Py_DECREF(array); // destroy the contig array
>>        Py_DECREF(input); // is this needed?
>
>            ^^^^^^^^^^^^^^^^^^^^
>
> Shouldn't be, but there might be a bug somewhere which causes the reference
> count of input to be double incremented. Does the reference count in the
> test script increase without this line? Do array and input point to the same
> object?
>

Oops, input in array are different types, so they won't point to the same
thing.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090417/add53dc4/attachment.html 


More information about the Numpy-discussion mailing list