Skip to content

Commit 01b22b4

Browse files
anishesgmeta-codesync[bot]
authored andcommitted
Fix memory leak warning for unordered_multimap in IndexIVFFlatDedup Python binding (#1667) (#5145)
Summary: When running the Python test suite, SWIG emits a warning: ``` swig/python detected a memory leak of type 'std::unordered_multimap< faiss::Index::idx_t,faiss::Index::idx_t > *', no destructor found. ``` The root cause is that `faiss::IndexIVFFlatDedup::instances` is a `std::unordered_multimap<idx_t, idx_t>` member, and SWIG has no built-in destructor registration for that STL type. When SWIG wraps the field, it generates a raw pointer wrapper with no cleanup, causing the leak warning whenever an `IndexIVFFlatDedup` object is garbage-collected on the Python side. The fix adds `%ignore faiss::IndexIVFFlatDedup::instances` in `faiss/python/swigfaiss.swig` before the `%include <faiss/IndexIVFFlat.h>` directive. This prevents SWIG from generating the untyped wrapper for the internal dedup map (which was never exposed in the Python type stubs anyway) and eliminates the memory leak warning. Fixes #1667 Pull Request resolved: #5145 Reviewed By: trang-nm-nguyen Differential Revision: D102668055 Pulled By: mnorris11 fbshipit-source-id: e865d9463aecc4d9c37447602b33e1cf71011007
1 parent a31dad3 commit 01b22b4

2 files changed

Lines changed: 4 additions & 1 deletion

File tree

faiss/python/swigfaiss.swig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,9 @@ void gpu_sync_all_devices()
569569
%include <faiss/impl/NNDescent.h>
570570
%include <faiss/IndexNNDescent.h>
571571

572+
// IndexIVFFlatDedup::instances is a std::unordered_multimap which SWIG has no
573+
// built-in destructor for; ignore the field to avoid memory leak warnings.
574+
%ignore faiss::IndexIVFFlatDedup::instances;
572575
%include <faiss/IndexIVFFlat.h>
573576
%include <faiss/IndexIVFFlatPanorama.h>
574577

tests/test_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_factory_3(self):
5454

5555
def test_factory_4(self):
5656
index = faiss.index_factory(12, "IVF10,FlatDedup")
57-
assert index.instances is not None
57+
assert isinstance(index, faiss.IndexIVFFlatDedup)
5858

5959
def test_factory_5(self):
6060
index = faiss.index_factory(128, "OPQ16,Flat")

0 commit comments

Comments
 (0)