From 918c113727da09082fbfa18232cfca676289fff0 Mon Sep 17 00:00:00 2001 From: Xu Peng Date: Wed, 24 Apr 2019 11:53:19 +0800 Subject: [PATCH] fix(db): fix memory leak Former-commit-id: 1c5b7e0ab18f9508aa5aeecfc7caecf5ac74fd45 --- cpp/src/db/DBImpl.cpp | 4 ++++ cpp/src/db/MemManager.cpp | 6 +----- cpp/src/db/MemManager.h | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cpp/src/db/DBImpl.cpp b/cpp/src/db/DBImpl.cpp index 19c708dd0b..2fc8e90400 100644 --- a/cpp/src/db/DBImpl.cpp +++ b/cpp/src/db/DBImpl.cpp @@ -89,6 +89,7 @@ Status DBImpl::search(const std::string &group_id, size_t k, size_t nq, index->add_with_ids(file_index->ntotal, dynamic_cast(file_index->index)->xb.data(), file_index->id_map.data()); + delete file_index; } { @@ -220,6 +221,7 @@ Status DBImpl::merge_files(const std::string& group_id, const meta::DateT& date, updated.push_back(file_schema); LOG(DEBUG) << "About to merge file " << file_schema.file_id << " of size=" << file_schema.rows; + delete file_index; } auto index_size = group_file.dimension * index->ntotal; @@ -301,6 +303,8 @@ Status DBImpl::build_index(const meta::GroupFileSchema& file) { meta::GroupFilesSchema update_files = {to_remove, group_file}; _pMeta->update_files(update_files); + delete from_index; + return Status::OK(); } diff --git a/cpp/src/db/MemManager.cpp b/cpp/src/db/MemManager.cpp index 59ae4fd19b..d1097f4072 100644 --- a/cpp/src/db/MemManager.cpp +++ b/cpp/src/db/MemManager.cpp @@ -47,7 +47,7 @@ Status MemVectors::serialize(std::string& group_id) { /* faiss::write_index(pIndex_, _file_location.c_str()); */ group_id = schema_.group_id; auto rows = approximate_size(); - write_index(pIndex_, schema_.location.c_str()); + write_index(pIndex_.get(), schema_.location.c_str()); schema_.rows = rows; schema_.file_type = (rows >= options_.index_trigger_size) ? meta::GroupFileSchema::TO_INDEX : meta::GroupFileSchema::RAW; @@ -61,10 +61,6 @@ MemVectors::~MemVectors() { delete _pIdGenerator; _pIdGenerator = nullptr; } - if (pIndex_ != nullptr) { - delete pIndex_; - pIndex_ = nullptr; - } } /* diff --git a/cpp/src/db/MemManager.h b/cpp/src/db/MemManager.h index a87c54c14e..c99e2be3c1 100644 --- a/cpp/src/db/MemManager.h +++ b/cpp/src/db/MemManager.h @@ -49,7 +49,7 @@ private: Options options_; meta::GroupFileSchema schema_; IDGenerator* _pIdGenerator; - faiss::Index* pIndex_; + std::shared_ptr pIndex_; }; // MemVectors