Skip to content

Commit 2b4a593

Browse files
authored
Fix memory leak in Context class (#269)
Fix memory leak in Context class Signed-off-by: Tomasz Szumski <tomasz.szumski@intel.com>
1 parent 3ade772 commit 2b4a593

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

media-proxy/include/mesh/concurrency.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ class Context {
5050
Context(Context& parent);
5151
Context(Context& parent, std::chrono::milliseconds timeout_ms);
5252

53-
Context *parent;
54-
thread::Channel<bool> *ch;
53+
Context *parent = nullptr;
54+
thread::Channel<bool> *ch = nullptr;
5555
std::future<void> async_cb;
5656
std::chrono::milliseconds timeout_ms;
57-
std::unique_ptr<std::stop_callback<std::function<void()>>> cb;
57+
std::unique_ptr<std::stop_callback<std::function<void()>>> cb = nullptr;
5858

5959
friend Context& Background();
6060
friend class WithCancel;

media-proxy/src/mesh/concurrency.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ namespace mesh {
1212
namespace context {
1313

1414
Context::Context() : ss(std::stop_source()),
15-
parent(nullptr),
16-
cb(nullptr),
17-
ch(new thread::Channel<bool>(1)),
1815
timeout_ms(std::chrono::milliseconds(0))
1916
{
2017
}
@@ -55,9 +52,11 @@ Context& Context::operator=(Context&& other) noexcept {
5552
ss = std::stop_source();
5653
parent = other.parent;
5754

58-
cb = std::make_unique<std::stop_callback<std::function<void()>>>(
59-
parent->ss.get_token(), [this] { cancel(); }
60-
);
55+
if (parent) {
56+
cb = std::make_unique<std::stop_callback<std::function<void()>>>(
57+
parent->ss.get_token(), [this] { cancel(); }
58+
);
59+
}
6160

6261
timeout_ms = other.timeout_ms;
6362

@@ -71,6 +70,10 @@ Context& Context::operator=(Context&& other) noexcept {
7170
});
7271
}
7372

73+
if (ch) {
74+
delete ch;
75+
}
76+
7477
ch = other.ch;
7578
other.ch = nullptr;
7679
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <gtest/gtest.h>
2+
#include "mesh/concurrency.h"
3+
4+
using namespace mesh;
5+
6+
class test_class {
7+
public:
8+
context::Context _ctx;
9+
10+
void init(context::Context &ctx) { _ctx = context::WithCancel(ctx); }
11+
};
12+
13+
/**
14+
* Test designed to detect memory leaks in the Context class.
15+
*
16+
* Run the test with Valgrind to detect memory leaks.
17+
* valgrind --leak-check=full ./media_proxy_unit_tests --gtest_filter=*Context*
18+
*/
19+
TEST(MeshContext, constructor) {
20+
auto ctx = context::WithCancel(context::Background());
21+
test_class t;
22+
t.init(ctx);
23+
{
24+
auto ctx2 = context::WithCancel(ctx);
25+
t.init(ctx2);
26+
mesh::thread::Sleep(ctx2, std::chrono::milliseconds(10));
27+
ctx2.cancel();
28+
}
29+
{
30+
context::Context c1;
31+
c1 = context::Context();
32+
}
33+
{
34+
context::Context c1 = context::WithTimeout(ctx, std::chrono::milliseconds(10));
35+
context::Context c2 = context::WithTimeout(c1, std::chrono::milliseconds(20));
36+
context::Context c3 = context::WithTimeout(c2, std::chrono::milliseconds(30));
37+
context::Context c4 = context::WithTimeout(c3, std::chrono::milliseconds(40));
38+
context::Context c5 = context::WithTimeout(c4, std::chrono::milliseconds(50));
39+
context::Context c6 = context::WithTimeout(c5, std::chrono::milliseconds(60));
40+
mesh::thread::Sleep(c6, std::chrono::milliseconds(10));
41+
}
42+
{
43+
context::Context c1 = context::WithTimeout(ctx, std::chrono::milliseconds(60));
44+
context::Context c2 = context::WithTimeout(c1, std::chrono::milliseconds(50));
45+
context::Context c3 = context::WithTimeout(c2, std::chrono::milliseconds(40));
46+
context::Context c4 = context::WithTimeout(c3, std::chrono::milliseconds(30));
47+
context::Context c5 = context::WithTimeout(c4, std::chrono::milliseconds(20));
48+
context::Context c6 = context::WithTimeout(c5, std::chrono::milliseconds(10));
49+
mesh::thread::Sleep(c6, std::chrono::milliseconds(10));
50+
}
51+
}

0 commit comments

Comments
 (0)