Skip to content

Commit 8911d9e

Browse files
committed
Fix memory leak on loadIndex with non-empty HierarchicalNSW object
1 parent 0df757e commit 8911d9e

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

hnswlib/hnswalg.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,20 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
139139

140140

141141
~HierarchicalNSW() {
142+
clear();
143+
}
144+
145+
void clear() {
142146
free(data_level0_memory_);
147+
data_level0_memory_ = nullptr;
143148
for (tableint i = 0; i < cur_element_count; i++) {
144149
if (element_levels_[i] > 0)
145150
free(linkLists_[i]);
146151
}
147152
free(linkLists_);
153+
linkLists_ = nullptr;
154+
cur_element_count = 0;
155+
visited_list_pool_.reset(nullptr);
148156
}
149157

150158

@@ -658,6 +666,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
658666
if (!input.is_open())
659667
throw std::runtime_error("Cannot open file");
660668

669+
clear();
661670
// get file size:
662671
input.seekg(0, input.end);
663672
std::streampos total_filesize = input.tellg();

0 commit comments

Comments
 (0)