Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in Context class #269

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}