From 07ea35020142a2ec273c565cf265fd57c346d302 Mon Sep 17 00:00:00 2001 From: airborne12 Date: Sun, 23 Apr 2023 23:21:44 +0800 Subject: [PATCH] [Fix](inverted index) fix memory leak when create bkd reader (#18914) The function compoundReader->openInput is called three times, and if any of these calls fail, an error is logged, and the function returns early. If one or two of the calls succeed, but the others fail, there might be a situation where the allocated memory for the IndexInput objects is not freed. To fix this, you could use std::unique_ptr to manage the memory for IndexInput objects. This would automatically clean up the memory when the function goes out of scope. --- .../segment_v2/inverted_index_compound_reader.cpp | 11 +++++++++++ .../segment_v2/inverted_index_compound_reader.h | 3 +++ .../olap/rowset/segment_v2/inverted_index_reader.cpp | 12 ++++++------ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.cpp b/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.cpp index 15646679cf..91c153f143 100644 --- a/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.cpp +++ b/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.cpp @@ -244,6 +244,17 @@ int64_t DorisCompoundReader::fileLength(const char* name) const { return e->length; } +bool DorisCompoundReader::openInput(const char* name, + std::unique_ptr& ret, + CLuceneError& error, int32_t bufferSize) { + lucene::store::IndexInput* tmp; + bool success = openInput(name, tmp, error, bufferSize); + if (success) { + ret.reset(tmp); + } + return success; +} + bool DorisCompoundReader::openInput(const char* name, lucene::store::IndexInput*& ret, CLuceneError& error, int32_t bufferSize) { if (stream == nullptr) { diff --git a/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.h b/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.h index 5c0d963247..75bf1ab633 100644 --- a/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.h +++ b/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.h @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -82,6 +83,8 @@ public: int64_t fileLength(const char* name) const override; bool openInput(const char* name, lucene::store::IndexInput*& ret, CLuceneError& err, int32_t bufferSize = -1) override; + bool openInput(const char* name, std::unique_ptr& ret, + CLuceneError& err, int32_t bufferSize = -1); void renameFile(const char* from, const char* to) override; void touchFile(const char* name) override; lucene::store::IndexOutput* createOutput(const char* name) override; diff --git a/be/src/olap/rowset/segment_v2/inverted_index_reader.cpp b/be/src/olap/rowset/segment_v2/inverted_index_reader.cpp index 6e94640255..a3c49c08a5 100644 --- a/be/src/olap/rowset/segment_v2/inverted_index_reader.cpp +++ b/be/src/olap/rowset/segment_v2/inverted_index_reader.cpp @@ -579,9 +579,9 @@ Status BkdIndexReader::get_bkd_reader(std::shared_ptr(); } CLuceneError err; - lucene::store::IndexInput* data_in; - lucene::store::IndexInput* meta_in; - lucene::store::IndexInput* index_in; + std::unique_ptr data_in; + std::unique_ptr meta_in; + std::unique_ptr index_in; if (!compoundReader->openInput( InvertedIndexDescriptor::get_temporary_bkd_index_data_file_name().c_str(), data_in, @@ -596,12 +596,12 @@ Status BkdIndexReader::get_bkd_reader(std::shared_ptr(); } - bkdReader = std::make_shared(data_in); - if (0 == bkdReader->read_meta(meta_in)) { + bkdReader = std::make_shared(data_in.release()); + if (0 == bkdReader->read_meta(meta_in.get())) { return Status::EndOfFile("bkd index file is empty"); } - bkdReader->read_index(index_in); + bkdReader->read_index(index_in.get()); _type_info = get_scalar_type_info((FieldType)bkdReader->type); if (_type_info == nullptr) {