Skip to content

Commit c55e502

Browse files
committed
Fix memory leak on loadIndex with non-empty HierarchicalNSW object
1 parent 802a0ec commit c55e502

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
@@ -138,13 +138,21 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
138138

139139

140140
~HierarchicalNSW() {
141+
clear();
142+
}
143+
144+
void clear() {
141145
free(data_level0_memory_);
146+
data_level0_memory_ = nullptr;
142147
for (tableint i = 0; i < cur_element_count; i++) {
143148
if (element_levels_[i] > 0)
144149
free(linkLists_[i]);
145150
}
146151
free(linkLists_);
152+
linkLists_ = nullptr;
153+
cur_element_count = 0;
147154
delete visited_list_pool_;
155+
visited_list_pool_ = nullptr;
148156
}
149157

150158

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

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

0 commit comments

Comments
 (0)