[Matrix-SIG] PyArray_Free in arrayobject.c

Travis Oliphant Oliphant.Travis@mayo.edu
Mon, 1 Mar 1999 18:28:26 -0600 (EST)

While I was looking at the source to write some documentation, I noticed
that PyArray_Free looks like it won't work as intended.  

It appears the PyArray_Free is supposed to work along with PyArray_As2D
and PyArray_As1D to dereference objects and free up memory that those two
functions create.

It probably works as intended for 1D arrays since it is called often after
use of PyArray_As1D throughout multiarraymodule.c and arrayobject.c

But, it doesn't look like it would work for 2D arrays since the following
code which deallocates the array of pointers created by PyArray_As2D never
gets called.

extern int PyArray_Free(PyObject *op, char *ptr) {
	PyArrayObject *ap = (PyArrayObject *)op;
	int i, n;
	if (ap->nd > 2) return -1;
	if (ap->nd == 3) {
        /* This statement won't be true if ap->nd <= 2  which it must be
in order to reach this point in the code.  So the following code never
gets called??*/
		n = ap->dimensions[0];
		for (i=0; i<n; i++) {
			free(((char **)ptr)[i]);
	if (ap->nd >= 2) {
	return 0;

Has anyone successfully used PyArray_Free along with PyArray_As2D.  For
now it looks like using PyArray_As2D springs a memory leak.  Anyone know
what's going on here.