Skip to content

Commit

Permalink
fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
zwhzzz0821 committed Nov 19, 2024
1 parent 9d55a19 commit fe2a217
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 15 deletions.
7 changes: 7 additions & 0 deletions cpp/src/reader/expression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,19 @@ Expression *QueryExpression::optimize(Expression *expression,
void QueryExpression::destory() {
for (size_t i = 0; i < my_exprs_.size(); i++) {
delete my_exprs_[i];
my_exprs_[i] = nullptr;
}
my_exprs_.clear();
for (size_t i = 0; i < my_filters_.size(); i++) {
delete my_filters_[i];
my_filters_[i] = nullptr;
}
if (expression_ != nullptr) {
delete expression_;
expression_ = nullptr;
}
my_filters_.clear();

}

} // namespace storage
16 changes: 16 additions & 0 deletions cpp/src/reader/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "common/db_common.h"
#include "common/path.h"
#include "filter/filter.h"

namespace storage {

Expand Down Expand Up @@ -64,6 +65,21 @@ struct Expression {
right_(nullptr),
filter_(f),
series_path_(path) {}
void destroy() {
if (filter_ != nullptr) {
delete filter_;
filter_ = nullptr;
}
if (left_ != nullptr) {
delete left_;
left_ = nullptr;
}
if (right_ != nullptr) {
delete right_;
right_ = nullptr;
}
}
~Expression() { destroy(); }
};

class QueryExpression {
Expand Down
4 changes: 4 additions & 0 deletions cpp/src/reader/qds_with_timegenerator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ void QDSWithTimeGenerator::close() {
for (size_t i = 0; i < value_at_vec_.size(); i++) {
value_at_vec_[i].destroy();
}
if (qe_ != nullptr) {
delete qe_;
qe_ = nullptr;
}
value_at_vec_.clear();
}

Expand Down
4 changes: 4 additions & 0 deletions cpp/src/reader/qds_without_timegenerator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ void QDSWithoutTimeGenerator::close() {
io_reader_->revert_ssi(ssi);
}
ssi_vec_.clear();
if (qe_ != nullptr) {
delete qe_;
qe_ = nullptr;
}
}

bool QDSWithoutTimeGenerator::next() {
Expand Down
6 changes: 4 additions & 2 deletions cpp/src/reader/tsfile_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ int TsFileReader::query(QueryExpression *qe, ResultSet *&ret_qds) {

int TsFileReader::query(std::vector<std::string> &path_list, int64_t start_time,
int64_t end_time, ResultSet *&result_set) {
int ret = E_OK;
Filter *time_filter = new TimeBetween(start_time, end_time, false);
Expression *exp =
new storage::Expression(storage::GLOBALTIME_EXPR, time_filter);
std::vector<Path> path_list_vec;
for (auto path : path_list) {
for (const auto &path : path_list) {
uint32_t last_point_pos = path.find_last_of('.');
if (last_point_pos <= 0) {
return E_INVALID_PATH;
Expand All @@ -79,7 +80,8 @@ int TsFileReader::query(std::vector<std::string> &path_list, int64_t start_time,
}
QueryExpression *query_expression =
QueryExpression::create(path_list_vec, exp);
return tsfile_executor_->execute(query_expression, result_set);
ret = tsfile_executor_->execute(query_expression, result_set);
return ret;
}

void TsFileReader::destroy_query_data_set(storage::ResultSet *qds) {
Expand Down
2 changes: 2 additions & 0 deletions cpp/test/reader/tsfile_reader_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ TEST_F(TsFileReaderTest, ResultSetMetadata) {
ASSERT_EQ(result_set_metadaa->get_column_name(0),
device_path + "." + measurement_name);
reader.destroy_query_data_set(qds);
reader.close();
}

TEST_F(TsFileReaderTest, GetAllDevice) {
Expand Down Expand Up @@ -204,4 +205,5 @@ TEST_F(TsFileReaderTest, GetTimeseriesSchema) {
reader.get_timeseries_schema(device_path[1], measurement_schemas);
ASSERT_EQ(measurement_schemas[1].measurement_name_, measurement_name[1]);
ASSERT_EQ(measurement_schemas[1].data_type_, TSDataType::INT32);
reader.close();
}
23 changes: 14 additions & 9 deletions cpp/test/writer/time_page_writer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,33 @@ TEST_F(TimePageWriterTest, WriteSuccess) {
}

TEST_F(TimePageWriterTest, WriteLargeDataSet) {
TimePageWriter page_writer;
page_writer.init(TSEncoding::PLAIN, UNCOMPRESSED);
for (int i = 0; i < 10000; ++i) {
page_writer_->write(i);
page_writer.write(i);
}
EXPECT_EQ(page_writer_->get_point_numer(), 10000);
EXPECT_EQ(page_writer.get_point_numer(), 10000);
}

TEST_F(TimePageWriterTest, ResetTimePageWriter) {
page_writer_->write(1234567890);
page_writer_->reset();
EXPECT_EQ(page_writer_->get_point_numer(), 0);
EXPECT_EQ(page_writer_->get_time_out_stream_size(), 0);
TimePageWriter page_writer;
page_writer.init(TSEncoding::PLAIN, UNCOMPRESSED);
page_writer.write(1234567890);
page_writer.reset();
EXPECT_EQ(page_writer.get_point_numer(), 0);
EXPECT_EQ(page_writer.get_time_out_stream_size(), 0);
}

TEST_F(TimePageWriterTest, DestroyTimePageWriter) {
page_writer_->write(1234567890);
TimeStatistic* stat = (TimeStatistic*)page_writer_->get_statistic();
TimePageWriter page_writer;
page_writer.init(TSEncoding::PLAIN, UNCOMPRESSED);
page_writer.write(1234567890);
TimeStatistic* stat = (TimeStatistic*)page_writer.get_statistic();
EXPECT_NE(stat, nullptr);
EXPECT_EQ(stat->count_, 1);
}

TEST_F(TimePageWriterTest, WritePageHeaderAndData) {
page_writer_->init(TSEncoding::PLAIN, UNCOMPRESSED);
EXPECT_EQ(page_writer_->write(1L), common::E_OK);
EXPECT_EQ(page_writer_->get_page_memory_size(), 8);
EXPECT_EQ(page_writer_->write(2L), common::E_OK);
Expand Down
5 changes: 1 addition & 4 deletions cpp/test/writer/tsfile_writer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ TEST_F(TsFileWriterTest, WriteDiffDataType) {
} while (true);
EXPECT_EQ(cur_record_num, row_num);
reader.destroy_query_data_set(qds);
reader.close();
}

TEST_F(TsFileWriterTest, RegisterTimeSeries) {
Expand Down Expand Up @@ -307,7 +308,6 @@ TEST_F(TsFileWriterTest, WriteMultipleTabletsMultiFlush) {
field_to_string(record->get_field(i)));
}
}
storage::QueryExpression::destory(query_expr);
reader.destroy_query_data_set(qds);
}

Expand Down Expand Up @@ -477,7 +477,6 @@ TEST_F(TsFileWriterTest, WriteAlignedTimeseries) {
field_to_string(record->get_field(i)));
}
}
storage::QueryExpression::destory(query_expr);
reader.destroy_query_data_set(qds);
}

Expand Down Expand Up @@ -542,7 +541,6 @@ TEST_F(TsFileWriterTest, WriteAlignedMultiFlush) {
field_to_string(record->get_field(i)));
}
}
storage::QueryExpression::destory(query_expr);
reader.destroy_query_data_set(qds);
}

Expand Down Expand Up @@ -608,6 +606,5 @@ TEST_F(TsFileWriterTest, WriteAlignedPartialData) {
}
cur_row++;
} while (true);
storage::QueryExpression::destory(query_expr);
reader.destroy_query_data_set(qds);
}

0 comments on commit fe2a217

Please sign in to comment.