From 1ea36298d2677733f36bae1cc6f72196c6395a4e Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Tue, 20 Feb 2018 17:08:46 +0000 Subject: Fix cUrlClient leak (#4125) Fixes #4040 * The TCP connection is now shutdown after OnBodyFinished * Any open connections are closed when cNetworkSingleton::Terminate() is called. * Removed ownership cycles in cUrlClientRequest * Added a check to the test to ensure there are no leaks. --- tests/HTTP/UrlClientTest.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'tests/HTTP/UrlClientTest.cpp') diff --git a/tests/HTTP/UrlClientTest.cpp b/tests/HTTP/UrlClientTest.cpp index f4a6f8f10..e86ad9156 100644 --- a/tests/HTTP/UrlClientTest.cpp +++ b/tests/HTTP/UrlClientTest.cpp @@ -6,6 +6,11 @@ +namespace +{ + +/** Track number of cCallbacks instances alive. */ +std::atomic g_ActiveCallbacks{ 0 }; /** Simple callbacks that dump the events to the console and signalize a cEvent when the request is finished. */ class cCallbacks: @@ -15,12 +20,14 @@ public: cCallbacks(cEvent & a_Event): m_Event(a_Event) { + ++g_ActiveCallbacks; LOGD("Created a cCallbacks instance at %p", reinterpret_cast(this)); } virtual ~cCallbacks() override { + --g_ActiveCallbacks; LOGD("Deleting the cCallbacks instance at %p", reinterpret_cast(this)); } @@ -102,7 +109,7 @@ protected: -static int TestRequest1() +int TestRequest1() { LOG("Running test 1"); cEvent evtFinished; @@ -126,7 +133,7 @@ static int TestRequest1() -static int TestRequest2() +int TestRequest2() { LOG("Running test 2"); cEvent evtFinished; @@ -148,7 +155,7 @@ static int TestRequest2() -static int TestRequest3() +int TestRequest3() { LOG("Running test 3"); cEvent evtFinished; @@ -172,7 +179,7 @@ static int TestRequest3() -static int TestRequest4() +int TestRequest4() { LOG("Running test 4"); cEvent evtFinished; @@ -194,7 +201,7 @@ static int TestRequest4() -static int TestRequests() +int TestRequests() { std::function tests[] = { @@ -215,6 +222,8 @@ static int TestRequests() return 0; } +} // namespace (anonymous) + @@ -231,6 +240,11 @@ int main() LOGD("Terminating cNetwork..."); cNetworkSingleton::Get().Terminate(); + + // No leaked callback instances + LOGD("cCallback instances still alive: %d", g_ActiveCallbacks.load()); + assert_test(g_ActiveCallbacks == 0); + LOGD("cUrlClient test finished"); return res; -- cgit v1.2.3