Kaydet (Commit) b803c517 authored tarafından Travis E. Oliphant's avatar Travis E. Oliphant

Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.

üst 30d1c51a
...@@ -145,9 +145,10 @@ typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *); ...@@ -145,9 +145,10 @@ typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
typedef struct bufferinfo { typedef struct bufferinfo {
void *buf; void *buf;
Py_ssize_t len; Py_ssize_t len;
Py_ssize_t itemsize; Py_ssize_t itemsize; /* This is Py_ssize_t so it can be
pointed to by strides in simple case.*/
int readonly; int readonly;
int ndim; /* XXX(nnorwitz): should be Py_ssize_t??? */ int ndim;
char *format; char *format;
Py_ssize_t *shape; Py_ssize_t *shape;
Py_ssize_t *strides; Py_ssize_t *strides;
......
...@@ -26,7 +26,7 @@ struct arraydescr { ...@@ -26,7 +26,7 @@ struct arraydescr {
int itemsize; int itemsize;
PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *); int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
const char *formats; char *formats;
}; };
typedef struct arrayobject { typedef struct arrayobject {
...@@ -42,8 +42,10 @@ static PyTypeObject Arraytype; ...@@ -42,8 +42,10 @@ static PyTypeObject Arraytype;
#ifdef Py_UNICODE_WIDE #ifdef Py_UNICODE_WIDE
#define PyArr_UNI 'w' #define PyArr_UNI 'w'
#define PyArr_UNISTR "w"
#else #else
#define PyArr_UNI 'u' #define PyArr_UNI 'u'
#define PyArr_UNISTR "u"
#endif #endif
#define array_Check(op) PyObject_TypeCheck(op, &Arraytype) #define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
...@@ -387,18 +389,18 @@ d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) ...@@ -387,18 +389,18 @@ d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
/* Description of types */ /* Description of types */
static struct arraydescr descriptors[] = { static struct arraydescr descriptors[] = {
{'b', sizeof(char), b_getitem, b_setitem}, {'b', sizeof(char), b_getitem, b_setitem, "b"},
{'B', sizeof(char), BB_getitem, BB_setitem}, {'B', sizeof(char), BB_getitem, BB_setitem, "B"},
{PyArr_UNI, sizeof(Py_UNICODE), u_getitem, u_setitem}, {PyArr_UNI, sizeof(Py_UNICODE), u_getitem, u_setitem, PyArr_UNISTR},
{'h', sizeof(short), h_getitem, h_setitem}, {'h', sizeof(short), h_getitem, h_setitem, "h"},
{'H', sizeof(short), HH_getitem, HH_setitem}, {'H', sizeof(short), HH_getitem, HH_setitem, "H"},
{'i', sizeof(int), i_getitem, i_setitem}, {'i', sizeof(int), i_getitem, i_setitem, "i"},
{'I', sizeof(int), II_getitem, II_setitem}, {'I', sizeof(int), II_getitem, II_setitem, "I"},
{'l', sizeof(long), l_getitem, l_setitem}, {'l', sizeof(long), l_getitem, l_setitem, "l"},
{'L', sizeof(long), LL_getitem, LL_setitem}, {'L', sizeof(long), LL_getitem, LL_setitem, "L"},
{'f', sizeof(float), f_getitem, f_setitem}, {'f', sizeof(float), f_getitem, f_setitem, "f"},
{'d', sizeof(double), d_getitem, d_setitem}, {'d', sizeof(double), d_getitem, d_setitem, "d"},
{'\0', 0, 0, 0} /* Sentinel */ {'\0', 0, 0, 0, 0} /* Sentinel */
}; };
/**************************************************************************** /****************************************************************************
...@@ -1770,12 +1772,7 @@ array_buffer_getbuf(arrayobject *self, PyBuffer *view, int flags) ...@@ -1770,12 +1772,7 @@ array_buffer_getbuf(arrayobject *self, PyBuffer *view, int flags)
view->format = NULL; view->format = NULL;
view->internal = NULL; view->internal = NULL;
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) { if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
view->internal = malloc(3); view->format = self->ob_descr->formats;
/* XXX(nnorwitz): need to check for malloc failure.
Should probably use PyObject_Malloc. */
view->format = view->internal;
view->format[0] = (char)(self->ob_descr->typecode);
view->format[1] = '\0';
} }
finish: finish:
...@@ -1786,7 +1783,6 @@ array_buffer_getbuf(arrayobject *self, PyBuffer *view, int flags) ...@@ -1786,7 +1783,6 @@ array_buffer_getbuf(arrayobject *self, PyBuffer *view, int flags)
static void static void
array_buffer_relbuf(arrayobject *self, PyBuffer *view) array_buffer_relbuf(arrayobject *self, PyBuffer *view)
{ {
free(view->internal);
self->ob_exports--; self->ob_exports--;
} }
......
...@@ -64,7 +64,6 @@ buffer_releasebuf(PyBufferObject *self, PyBuffer *view) ...@@ -64,7 +64,6 @@ buffer_releasebuf(PyBufferObject *self, PyBuffer *view)
(*bp->bf_releasebuffer)(self->b_base, view); (*bp->bf_releasebuffer)(self->b_base, view);
} }
} }
/* XXX(nnorwitz): do we need to release view here? it leaks. */
} }
static PyObject * static PyObject *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment