Skip to content

Commit

Permalink
Fix memory leak on loadIndex with non-empty HierarchicalNSW object
Browse files Browse the repository at this point in the history
  • Loading branch information
drons committed Jul 5, 2023
1 parent 0df757e commit 8911d9e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions hnswlib/hnswalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,20 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {


~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);
}


Expand Down Expand Up @@ -658,6 +666,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
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();
Expand Down

0 comments on commit 8911d9e

Please sign in to comment.