From 3f9e8b199f9fd48c3d54ea692b558d001a5f8982 Mon Sep 17 00:00:00 2001 From: Erik Bernhardsson Date: Sun, 9 Apr 2023 22:22:39 -0400 Subject: [PATCH 1/2] fix memory leak --- src/annoymodule.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/annoymodule.cc b/src/annoymodule.cc index c106f5bf..6bb0ae1b 100644 --- a/src/annoymodule.cc +++ b/src/annoymodule.cc @@ -270,6 +270,8 @@ get_nns_to_python(const vector& result, const vector& distances, if ((t = PyTuple_Pack(2, l, d)) == NULL) { goto error; } + Py_XDECREF(l); + Py_XDECREF(d); return t; From daf2caa6f893ce02da62495afdf6c50ce536c369 Mon Sep 17 00:00:00 2001 From: Erik Bernhardsson Date: Mon, 10 Apr 2023 09:33:25 -0400 Subject: [PATCH 2/2] add test --- test/memory_leak_test.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/memory_leak_test.py b/test/memory_leak_test.py index 93d4f4fa..64349f81 100644 --- a/test/memory_leak_test.py +++ b/test/memory_leak_test.py @@ -12,6 +12,7 @@ # License for the specific language governing permissions and limitations under # the License. +import pytest import random from annoy import AnnoyIndex @@ -48,3 +49,17 @@ def test_build_unbuid(): i.build(10) assert i.get_n_items() == 1000 + + +def test_include_distances(): + # See #633 + # (Not able to repro it though) + f = 10 + i = AnnoyIndex(f, "euclidean") + for j in range(10000): + i.add_item(j, [random.gauss(0, 1) for x in range(f)]) + i.build(10) + + v = [random.gauss(0, 1) for x in range(f)] + for _ in range(10000000): + indices, distances = i.get_nns_by_vector(v, 1, include_distances=True)