Skip to content

Commit

Permalink
Merge pull request #639 from spotify/erikbern/maybe-fix-memory-leak
Browse files Browse the repository at this point in the history
Decref to object we got
  • Loading branch information
erikbern authored Apr 10, 2023
2 parents 0a338cd + daf2caa commit a07611b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/annoymodule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ get_nns_to_python(const vector<int32_t>& result, const vector<float>& distances,
if ((t = PyTuple_Pack(2, l, d)) == NULL) {
goto error;
}
Py_XDECREF(l);
Py_XDECREF(d);

return t;

Expand Down
15 changes: 15 additions & 0 deletions test/memory_leak_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations under
# the License.

import pytest
import random

from annoy import AnnoyIndex
Expand Down Expand Up @@ -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)

0 comments on commit a07611b

Please sign in to comment.