From 4f2e80d649b74e3a8551bea83d53d2d00bf7c63f Mon Sep 17 00:00:00 2001 From: Georgiy Lebedev Date: Wed, 22 Nov 2023 15:06:44 +0300 Subject: [PATCH] Test: fix memory leak in ListUnitTest Fix memory leaks `test_massive()` scenario of ListUnitTest caused by the list elements not being cleaned up after the test. To fix this in a simple way, use references to `vector` elements as list elements instead of explicit dynamic allocation. --- test/ListUnitTest.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/test/ListUnitTest.cpp b/test/ListUnitTest.cpp index 12d538c1e..4b5c28540 100644 --- a/test/ListUnitTest.cpp +++ b/test/ListUnitTest.cpp @@ -40,6 +40,10 @@ struct Object : tnt::SingleLink { int m_Data; + Object() : m_Data(0) + { + } + Object(int id) : m_Data(id) { } @@ -1028,6 +1032,7 @@ void test_massive() std::vector sReference; const size_t ITER_COUNT = 100000; const size_t SIZE_LIM = 10; + std::vector objects(ITER_COUNT); for (size_t i = 0; i < ITER_COUNT; i++) { bool sAdd = true; if (sReference.size() == SIZE_LIM) @@ -1037,20 +1042,20 @@ void test_massive() bool sBegin = (rand() & 1) == 0; if (sAdd) { int sValue = rand(); - Object *sObj = new Object(sValue); + objects[i].m_Data = sValue; if (sBegin) { - list.insert(*sObj); + list.insert(objects[i]); sReference.insert(sReference.begin(), sValue); } else { - list.insert(*sObj, true); + list.insert(objects[i], true); sReference.insert(sReference.end(), sValue); } } else { if (sBegin) { - delete &list.front(); + list.front().remove(); sReference.erase(sReference.begin()); } else { - delete &list.back(); + list.back().remove(); sReference.pop_back(); } }