[Numpy-discussion] slice question and bug

Todd Miller jmiller at stsci.edu
Thu Apr 11 14:36:03 EDT 2002

clee at spiralis.merseine.nu wrote:

>clee at spiralis.merseine.nu writes:
> > 
> > Hello,
> > I'm trying to track down a segv when I do the B[:] operation on an
> > array, "B", a that I've built in as a view on external data.  During...
> > [snip]
>To clarify my own somewhat non-sensical post: When I started composing
>my message, I was trying to figure out a bug in my own code that
>caused a crash while doing slice_array.  I've since fixed that bug.
>However, in the process of figuring out what I was doing wrong I
>was browsing the Numeric source code.  While examining
>PyArray_Free(..) in arrayobject.c, I saw that returns -1 whenever the
>number of dimensions is greater than 2, yet it has code that tests for
>when the number of dimensions equals 3.
>So utimately, my post is just an alert, that I think there might be
>some code that needs to be cleaned up. 
> lacking-caffeine-ly yours
> -chris 
>Numpy-discussion mailing list
>Numpy-discussion at lists.sourceforge.net
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) {
	 return 0;
/* {%c++%} */

Other opinions?


Todd Miller 			jmiller at stsci.edu
STSCI / SSG			(410) 338 4576

More information about the NumPy-Discussion mailing list