[Numpy-discussion] Some questions about PyArray_As2D.

Andrea Riciputi ariciputi at pito.com
Fri Dec 13 02:46:00 EST 2002


On Wednesday, Dec 11, 2002, at 19:43 Europe/Rome, Konrad Hinsen wrote:

> No. op is an input parameter and thus a "borrowed" reference. It might
> not be the best coding style to reuse that variable name for something
> unrelated later on, but it doesn't cause a memory leak.

I'm sorry but I don't agree. I've read your answer many times and 
re-read the code but I still think to be right.

The function prototype says:

extern int PyArray_As2D(PyObject **op, char ***ptr, int *d1, int *d2, 
int typecode)

and a call to this function looks like this:

PyObject *input;
double **result;
int nrows, ncols;

PyArray_As2D((&input, (char ***) &(result), &(nrows), &(ncols), 
PyArray_DOUBLE)

Now when you call the function in this way op is a pointer to the 
pointer that points to your original ArrayObject. It allows you to 
change the memory address which is originally pointed by input. And it 
is exactly what you do with the instruction:

*op = (PyObject *)ap;

So you create a new PyArrayObject (allocating another memory area) by 
means of PyArray_ContiguousFromObject and names it ap, then you modify 
the memory address which op points to with the above instruction. Now 
*op (that is input) points to another memory region, but you haven't 
deallocated the previous pointed memory and it _is_ a memory leak!

These are my two cents, comments are welcome.

Cheers,
Andrea.


---
Andrea Riciputi        <mailto:andrea.riciputi at libero.it>

"Science is like sex: sometimes something useful comes out,
   but that is not the reason we are doing it" -- (Richard Feynman)





More information about the NumPy-Discussion mailing list