Skip to content

Commit

Permalink
Fix memory leak in Context class (#269)
Browse files Browse the repository at this point in the history
Fix memory leak in Context class

Signed-off-by: Tomasz Szumski <tomasz.szumski@intel.com>
  • Loading branch information
tszumski authored Nov 27, 2024
1 parent 3ade772 commit 2b4a593
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
6 changes: 3 additions & 3 deletions media-proxy/include/mesh/concurrency.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ class Context {
Context(Context& parent);
Context(Context& parent, std::chrono::milliseconds timeout_ms);

Context *parent;
thread::Channel<bool> *ch;
Context *parent = nullptr;
thread::Channel<bool> *ch = nullptr;
std::future<void> async_cb;
std::chrono::milliseconds timeout_ms;
std::unique_ptr<std::stop_callback<std::function<void()>>> cb;
std::unique_ptr<std::stop_callback<std::function<void()>>> cb = nullptr;

friend Context& Background();
friend class WithCancel;
Expand Down
15 changes: 9 additions & 6 deletions media-proxy/src/mesh/concurrency.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ namespace mesh {
namespace context {

Context::Context() : ss(std::stop_source()),
parent(nullptr),
cb(nullptr),
ch(new thread::Channel<bool>(1)),
timeout_ms(std::chrono::milliseconds(0))
{
}
Expand Down Expand Up @@ -55,9 +52,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 +70,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(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 2b4a593

Please sign in to comment.