Skip to content

Commit

Permalink
Test: fix memory leak in ListUnitTest
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CuriousGeorgiy committed Nov 22, 2023
1 parent dd816aa commit 728dc93
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions test/ListUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ struct Object : tnt::SingleLink<Object>
{
int m_Data;

Object() : m_Data(0)
{
}

Object(int id) : m_Data(id)
{
}
Expand Down Expand Up @@ -1028,6 +1032,7 @@ void test_massive()
std::vector<int> sReference;
const size_t ITER_COUNT = 100000;
const size_t SIZE_LIM = 10;
std::vector<Object> objects(ITER_COUNT);
for (size_t i = 0; i < ITER_COUNT; i++) {
bool sAdd = true;
if (sReference.size() == SIZE_LIM)
Expand All @@ -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();
}
}
Expand Down

0 comments on commit 728dc93

Please sign in to comment.