From 8911d9ef64c60037b8b0a2991d46a1caea6f1c23 Mon Sep 17 00:00:00 2001 From: drons Date: Sun, 2 Jul 2023 14:53:24 +0300 Subject: [PATCH] Fix memory leak on loadIndex with non-empty HierarchicalNSW object --- hnswlib/hnswalg.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/hnswlib/hnswalg.h b/hnswlib/hnswalg.h index 901b1878..493e55fe 100644 --- a/hnswlib/hnswalg.h +++ b/hnswlib/hnswalg.h @@ -139,12 +139,20 @@ class HierarchicalNSW : public AlgorithmInterface { ~HierarchicalNSW() { + clear(); + } + + void clear() { free(data_level0_memory_); + data_level0_memory_ = nullptr; for (tableint i = 0; i < cur_element_count; i++) { if (element_levels_[i] > 0) free(linkLists_[i]); } free(linkLists_); + linkLists_ = nullptr; + cur_element_count = 0; + visited_list_pool_.reset(nullptr); } @@ -658,6 +666,7 @@ class HierarchicalNSW : public AlgorithmInterface { if (!input.is_open()) throw std::runtime_error("Cannot open file"); + clear(); // get file size: input.seekg(0, input.end); std::streampos total_filesize = input.tellg();