Skip to content

Commit

Permalink
Fix memory leak in Context class
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Szumski <tomasz.szumski@intel.com>
  • Loading branch information
tszumski committed Nov 27, 2024
1 parent 3ade772 commit 82bb2cb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
14 changes: 10 additions & 4 deletions media-proxy/src/mesh/concurrency.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace context {
Context::Context() : ss(std::stop_source()),
parent(nullptr),
cb(nullptr),
ch(new thread::Channel<bool>(1)),
ch(nullptr),
timeout_ms(std::chrono::milliseconds(0))
{
}
Expand Down Expand Up @@ -55,9 +55,11 @@ Context& Context::operator=(Context&& other) noexcept {
ss = std::stop_source();
parent = other.parent;

cb = std::make_unique<std::stop_callback<std::function<void()>>>(
parent->ss.get_token(), [this] { cancel(); }
);
if(parent){
cb = std::make_unique<std::stop_callback<std::function<void()>>>(
parent->ss.get_token(), [this] { cancel(); }
);
}

timeout_ms = other.timeout_ms;

Expand All @@ -71,6 +73,10 @@ Context& Context::operator=(Context&& other) noexcept {
});
}

if (ch) {
delete ch;
}

ch = other.ch;
other.ch = nullptr;
}
Expand Down
51 changes: 51 additions & 0 deletions media-proxy/tests/mesh_context_tests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <gtest/gtest.h>
#include "mesh/concurrency.h"

using namespace mesh;

class test_class {
public:
context::Context _ctx;

void init(context::Context &ctx) { _ctx = context::WithCancel(ctx); }
};

/**
* Test designed to detect memory leaks in the Context class.
*
* Run the test with Valgrind to detect memory leaks.
* valgrind --leak-check=full ./media_proxy_unit_tests --gtest_filter=*Context*
*/
TEST(MeshContext, constructor) {
auto ctx = context::WithCancel(mesh::context::Background());
test_class t;
t.init(ctx);
{
auto ctx2 = context::WithCancel(ctx);
t.init(ctx2);
mesh::thread::Sleep(ctx2, std::chrono::milliseconds(10));
ctx2.cancel();
}
{
context::Context c1;
c1 = context::Context();
}
{
context::Context c1 = context::WithTimeout(ctx, std::chrono::milliseconds(10));
context::Context c2 = context::WithTimeout(c1, std::chrono::milliseconds(20));
context::Context c3 = context::WithTimeout(c2, std::chrono::milliseconds(30));
context::Context c4 = context::WithTimeout(c3, std::chrono::milliseconds(40));
context::Context c5 = context::WithTimeout(c4, std::chrono::milliseconds(50));
context::Context c6 = context::WithTimeout(c5, std::chrono::milliseconds(60));
mesh::thread::Sleep(c6, std::chrono::milliseconds(10));
}
{
context::Context c1 = context::WithTimeout(ctx, std::chrono::milliseconds(60));
context::Context c2 = context::WithTimeout(c1, std::chrono::milliseconds(50));
context::Context c3 = context::WithTimeout(c2, std::chrono::milliseconds(40));
context::Context c4 = context::WithTimeout(c3, std::chrono::milliseconds(30));
context::Context c5 = context::WithTimeout(c4, std::chrono::milliseconds(20));
context::Context c6 = context::WithTimeout(c5, std::chrono::milliseconds(10));
mesh::thread::Sleep(c6, std::chrono::milliseconds(10));
}
}

0 comments on commit 82bb2cb

Please sign in to comment.