diff options
author | Matti Picus <matti.picus@gmail.com> | 2018-06-05 11:21:26 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-05 11:21:26 -0700 |
commit | 5b8b1349ed194516b5e1804b63ff4266bca286ab (patch) | |
tree | 9304d97329c2bd3309fdc3deaf6a8363fa6953fa /numpy | |
parent | 7e352df0934b33a640d6c2e054cf7e90028b7566 (diff) | |
parent | 644c78809ff5b53b0fcdbb35e5823fc6e01d24ac (diff) | |
download | numpy-5b8b1349ed194516b5e1804b63ff4266bca286ab.tar.gz |
Merge pull request #11247 from eric-wieser/simplify-incref-handling
MAINT/BUG: Remove out-of-band reference count in PyArray_Newshape, fix memory leak
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/shape.c | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/numpy/core/src/multiarray/shape.c b/numpy/core/src/multiarray/shape.c index dec9eedc9..1424a69f3 100644 --- a/numpy/core/src/multiarray/shape.c +++ b/numpy/core/src/multiarray/shape.c @@ -189,7 +189,7 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, npy_intp *dimensions = newdims->ptr; PyArrayObject *ret; int ndim = newdims->len; - npy_bool same, incref = NPY_TRUE; + npy_bool same; npy_intp *strides = NULL; npy_intp newstrides[NPY_MAXDIMS]; int flags; @@ -230,6 +230,7 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, * data in the order it is in. * NPY_RELAXED_STRIDES_CHECKING: size check is unnecessary when set. */ + Py_INCREF(self); if ((PyArray_SIZE(self) > 1) && ((order == NPY_CORDER && !PyArray_IS_C_CONTIGUOUS(self)) || (order == NPY_FORTRANORDER && !PyArray_IS_F_CONTIGUOUS(self)))) { @@ -243,10 +244,10 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, else { PyObject *newcopy; newcopy = PyArray_NewCopy(self, order); + Py_DECREF(self); if (newcopy == NULL) { return NULL; } - incref = NPY_FALSE; self = (PyArrayObject *)newcopy; } } @@ -277,9 +278,6 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, goto fail; } - if (incref) { - Py_INCREF(self); - } if (PyArray_SetBaseObject(ret, (PyObject *)self)) { Py_DECREF(ret); return NULL; @@ -287,9 +285,7 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, return (PyObject *)ret; fail: - if (!incref) { - Py_DECREF(self); - } + Py_DECREF(self); return NULL; } |