[Numpy-discussion] slice question and bug

Travis Oliphant oliphant at ee.byu.edu
Thu Apr 11 15:47:04 CDT 2002


> Looking at the code to PyArray_Free,  I agree with Chris.  Called to
> free a 2D
> array, I think that PyArray_Free leaks all of the row storage because
> ap->nd == 2, not 3:
>
> * {%c++%} */
>      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) {
> 	     n = ap->dimensions[0];
> 	     for (i=0; i<n; i++) {
> 		 free(((char **)ptr)[i]);
> 	     }
> 	 }
> 	 if (ap->nd >= 2) {
> 	     free(ptr);
> 	 }
> 	 Py_DECREF(ap);
> 	 return 0;
>      }
> /* {%c++%} */
>
>

This has been broken since the beginning.  I believe the documentation
says as much.  I've never used it because I always think of 2-D arrays as
a block of data not as rows of pointers.

It should be fixed, but no one's ever been interested enough to do it.

-Travis Oliphant






More information about the Numpy-discussion mailing list