From 764f89dc7ee9ccd8d57f02c28f47e4da160f75ae Mon Sep 17 00:00:00 2001 From: Joel Billaud Date: Thu, 3 May 2018 08:57:27 +0900 Subject: [PATCH] Fix memory leak. PyArray_UpdateFlags silently ignores the NPY_OWNDATA flag. --- sklearn/neighbors/binary_tree.pxi | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sklearn/neighbors/binary_tree.pxi b/sklearn/neighbors/binary_tree.pxi index 7f067021df638..edf78257c9b23 100755 --- a/sklearn/neighbors/binary_tree.pxi +++ b/sklearn/neighbors/binary_tree.pxi @@ -158,6 +158,9 @@ from typedefs import DTYPE, ITYPE from dist_metrics cimport (DistanceMetric, euclidean_dist, euclidean_rdist, euclidean_dist_to_rdist, euclidean_rdist_to_dist) +cdef extern from "numpy/arrayobject.h": + void PyArray_ENABLEFLAGS(np.ndarray arr, int flags) + np.import_array() # some handy constants @@ -1501,14 +1504,14 @@ cdef class BinaryTree: # make a new numpy array that wraps the existing data indices_npy[i] = np.PyArray_SimpleNewFromData(1, &counts[i], np.NPY_INTP, indices[i]) # make sure the data will be freed when the numpy array is garbage collected - np.PyArray_UpdateFlags(indices_npy[i], indices_npy[i].flags.num | np.NPY_OWNDATA) + PyArray_ENABLEFLAGS(indices_npy[i], np.NPY_OWNDATA) # make sure the data is not freed twice indices[i] = NULL # make a new numpy array that wraps the existing data distances_npy[i] = np.PyArray_SimpleNewFromData(1, &counts[i], np.NPY_DOUBLE, distances[i]) # make sure the data will be freed when the numpy array is garbage collected - np.PyArray_UpdateFlags(distances_npy[i], distances_npy[i].flags.num | np.NPY_OWNDATA) + PyArray_ENABLEFLAGS(distances_npy[i], np.NPY_OWNDATA) # make sure the data is not freed twice distances[i] = NULL @@ -1521,7 +1524,7 @@ cdef class BinaryTree: # make a new numpy array that wraps the existing data indices_npy[i] = np.PyArray_SimpleNewFromData(1, &counts[i], np.NPY_INTP, indices[i]) # make sure the data will be freed when the numpy array is garbage collected - np.PyArray_UpdateFlags(indices_npy[i], indices_npy[i].flags.num | np.NPY_OWNDATA) + PyArray_ENABLEFLAGS(indices_npy[i], np.NPY_OWNDATA) # make sure the data is not freed twice indices[i] = NULL