Skip to content

Fix radius neighbors memory leak. #11056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions sklearn/neighbors/binary_tree.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down