From ef6cccf453ae89d3f86bf2af9ed56dbaac3e613e Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Wed, 23 Mar 2022 17:59:26 +0900 Subject: [PATCH] Release timeout resource appropriately Change-Id: I4b32aa7e51f6c50649439e9c69980b7296718ebd Signed-off-by: hyunuk.tak --- tests/unittest/mocks/thread-mock-dummy.cpp | 9 ++++++++- tests/unittest/mocks/thread-mock-util.cpp | 23 +++++++++++++---------- tests/unittest/mocks/thread-mock-util.h | 2 ++ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/tests/unittest/mocks/thread-mock-dummy.cpp b/tests/unittest/mocks/thread-mock-dummy.cpp index bbb950d..e5b7465 100644 --- a/tests/unittest/mocks/thread-mock-dummy.cpp +++ b/tests/unittest/mocks/thread-mock-dummy.cpp @@ -23,6 +23,7 @@ typedef struct { GAsyncReadyCallback callback; GVariant *result; gpointer user_data; + guint timeout_id; } gdbus_result_data_s; static gdbus_result_data_s _gdbus_async_result; @@ -92,7 +93,8 @@ static void __method_scan(GVariant *parameters, _gdbus_async_result.callback = callback; _gdbus_async_result.result = __make_scan_result(); _gdbus_async_result.user_data = user_data; - g_timeout_add(ASYNC_RESULT_DELAY, __reply_async_method, &_gdbus_async_result); + _gdbus_async_result.timeout_id = g_timeout_add( + ASYNC_RESULT_DELAY, __reply_async_method, &_gdbus_async_result); } static GVariant *__property_device_role() @@ -275,5 +277,10 @@ GVariant *_handle_property(const gchar *method_name, GVariant *_get_gdbus_async_result(GError **error) { *error = nullptr; + if (_gdbus_async_result.timeout_id > 0) + { + g_source_remove(_gdbus_async_result.timeout_id); + _gdbus_async_result.timeout_id = 0; + } return _gdbus_async_result.result; } \ No newline at end of file diff --git a/tests/unittest/mocks/thread-mock-util.cpp b/tests/unittest/mocks/thread-mock-util.cpp index 84d2aae..e6d2fb7 100644 --- a/tests/unittest/mocks/thread-mock-util.cpp +++ b/tests/unittest/mocks/thread-mock-util.cpp @@ -16,15 +16,6 @@ #include "thread-mock-util.h" -static gboolean _timeoutCallback(gpointer data) -{ - GMainLoop *mainLoop = NULL; - mainLoop = (GMainLoop *)data; - if (mainLoop != NULL) - g_main_loop_quit(mainLoop); - return FALSE; -} - void ScanCallback(int result, thread_network_scanning_state_e state, uint64_t ext_address, const char *network_name, uint64_t ext_panidi, const uint8_t *steering_data, int length, uint16_t panid, uint16_t joiner_udp_port, uint16_t channel, @@ -34,10 +25,22 @@ void ScanCallback(int result, thread_network_scanning_state_e state, QUIT_GMAIN_LOOP; } +gboolean ThreadMainLoop::timeoutCallback(gpointer data) +{ + GMainLoop *mainLoop = NULL; + mainLoop = (GMainLoop *)data; + if (mainLoop != NULL) + { + g_main_loop_quit(mainLoop); + THREAD_MAIN_LOOP.timeoutId = 0; + } + return FALSE; +} + void ThreadMainLoop::RunMainLoop() { mainLoop = g_main_loop_new(NULL, false); - timeoutId = g_timeout_add(CALLBACK_TIMEOUT, _timeoutCallback, mainLoop); + timeoutId = g_timeout_add(CALLBACK_TIMEOUT, timeoutCallback, mainLoop); g_main_loop_run(mainLoop); if (timeoutId > 0) g_source_remove(timeoutId); diff --git a/tests/unittest/mocks/thread-mock-util.h b/tests/unittest/mocks/thread-mock-util.h index 2f5fa08..bd9cc76 100644 --- a/tests/unittest/mocks/thread-mock-util.h +++ b/tests/unittest/mocks/thread-mock-util.h @@ -48,6 +48,8 @@ private: ThreadMainLoop(const ThreadMainLoop&) = delete; ThreadMainLoop& operator=(const ThreadMainLoop&) = delete; + static gboolean timeoutCallback(gpointer data); + private: GMainLoop *mainLoop; guint timeoutId; -- 2.7.4