From 3197766dc704a47ecf8d6794d3f4998fa6640f71 Mon Sep 17 00:00:00 2001 From: Adonis Ling Date: Fri, 11 Feb 2022 14:41:01 +0800 Subject: [PATCH 1/2] [chore] Fix memory problems in agg_test.cpp. --- be/test/vec/aggregate_functions/agg_test.cpp | 24 +++++++++----------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/be/test/vec/aggregate_functions/agg_test.cpp b/be/test/vec/aggregate_functions/agg_test.cpp index f52fc655ede7e4..f908da1730ce2e 100644 --- a/be/test/vec/aggregate_functions/agg_test.cpp +++ b/be/test/vec/aggregate_functions/agg_test.cpp @@ -15,6 +15,7 @@ // specific language governing permissions and limitations // under the License. +#include #include #include "gtest/gtest.h" @@ -45,21 +46,18 @@ TEST(AggTest, basic_test) { DataTypes data_types = {data_type}; Array array; auto agg_function = factory.get("sum", data_types, array); - AggregateDataPtr place = new char[agg_function->size_of_data()]; - agg_function->create(place); + std::unique_ptr place(new char[agg_function->size_of_data()]); + agg_function->create(place.get()); const IColumn* column[1] = {column_vector_int32.get()}; for (int i = 0; i < agg_test_batch_size; i++) { - agg_function->add(place, column, i, nullptr); + agg_function->add(place.get(), column, i, nullptr); } int ans = 0; for (int i = 0; i < agg_test_batch_size; i++) { ans += i; } - ASSERT_EQ(ans, *(int32_t*)place); - agg_function->destroy(place); - if (place) { - free(place); - } + ASSERT_EQ(ans, *reinterpret_cast(place.get())); + agg_function->destroy(place.get()); } TEST(AggTest, topn_test) { @@ -80,21 +78,21 @@ TEST(AggTest, topn_test) { Array array; auto agg_function = factory.get("topn", data_types, array); - AggregateDataPtr place = new char[agg_function->size_of_data()]; - agg_function->create(place); + std::unique_ptr place(new char[agg_function->size_of_data()]); + agg_function->create(place.get()); IColumn* columns[2] = {datas[0].get(), datas[1].get()}; for (int i = 0; i < agg_test_batch_size; i++) { - agg_function->add(place, const_cast(columns), i, nullptr); + agg_function->add(place.get(), const_cast(columns), i, nullptr); } - std::string result = reinterpret_cast(place)->get(); + std::string result = reinterpret_cast(place.get())->get(); std::string expect_result = "{\"1\":2048,\"2\":683,\"3\":341,\"4\":205,\"5\":137,\"6\":97,\"7\":73,\"8\":57,\"9\":" "46,\"10\":37}"; ASSERT_EQ(result, expect_result); - agg_function->destroy(place); + agg_function->destroy(place.get()); } } // namespace doris::vectorized From b7ff7c9f835c4dca912d52098b901b7c80eb11f2 Mon Sep 17 00:00:00 2001 From: Adonis Ling Date: Fri, 11 Feb 2022 16:19:16 +0800 Subject: [PATCH 2/2] Simplify the modification. --- be/test/vec/aggregate_functions/agg_test.cpp | 22 +++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/be/test/vec/aggregate_functions/agg_test.cpp b/be/test/vec/aggregate_functions/agg_test.cpp index f908da1730ce2e..b0d3c0656415ab 100644 --- a/be/test/vec/aggregate_functions/agg_test.cpp +++ b/be/test/vec/aggregate_functions/agg_test.cpp @@ -46,18 +46,19 @@ TEST(AggTest, basic_test) { DataTypes data_types = {data_type}; Array array; auto agg_function = factory.get("sum", data_types, array); - std::unique_ptr place(new char[agg_function->size_of_data()]); - agg_function->create(place.get()); + std::unique_ptr memory(new char[agg_function->size_of_data()]); + AggregateDataPtr place = memory.get(); + agg_function->create(place); const IColumn* column[1] = {column_vector_int32.get()}; for (int i = 0; i < agg_test_batch_size; i++) { - agg_function->add(place.get(), column, i, nullptr); + agg_function->add(place, column, i, nullptr); } int ans = 0; for (int i = 0; i < agg_test_batch_size; i++) { ans += i; } - ASSERT_EQ(ans, *reinterpret_cast(place.get())); - agg_function->destroy(place.get()); + ASSERT_EQ(ans, *reinterpret_cast(place)); + agg_function->destroy(place); } TEST(AggTest, topn_test) { @@ -78,21 +79,22 @@ TEST(AggTest, topn_test) { Array array; auto agg_function = factory.get("topn", data_types, array); - std::unique_ptr place(new char[agg_function->size_of_data()]); - agg_function->create(place.get()); + std::unique_ptr memory(new char[agg_function->size_of_data()]); + AggregateDataPtr place = memory.get(); + agg_function->create(place); IColumn* columns[2] = {datas[0].get(), datas[1].get()}; for (int i = 0; i < agg_test_batch_size; i++) { - agg_function->add(place.get(), const_cast(columns), i, nullptr); + agg_function->add(place, const_cast(columns), i, nullptr); } - std::string result = reinterpret_cast(place.get())->get(); + std::string result = reinterpret_cast(place)->get(); std::string expect_result = "{\"1\":2048,\"2\":683,\"3\":341,\"4\":205,\"5\":137,\"6\":97,\"7\":73,\"8\":57,\"9\":" "46,\"10\":37}"; ASSERT_EQ(result, expect_result); - agg_function->destroy(place.get()); + agg_function->destroy(place); } } // namespace doris::vectorized