From 2a757f4ede2ef24ce6b3a48d42677e6732335e0b Mon Sep 17 00:00:00 2001 From: Senthil Kumar G S Date: Wed, 14 Aug 2019 20:01:06 +0530 Subject: [PATCH] [CONPRO-1476] Fix memory leak in TCP server. On starting TCP server, CAReceiveHandler thread is created but it is not destroyed when it is stopped. Though the thread stops asynchronously, its handle which is stored in the thread pool is not removed and destroyed during stop which causes memory leak to persist till stack termination. Note: Cherry-picked the patch from https://github.sec.samsung.net/SES/SmartThingsThing/commit/c1d8cf83510f988fb4f8bf0e6b72cb18f618d4ec Reviewed & Verified on current 1.2-rel codebase. https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/commit/ced355b7c49a76966fe4961ef93f53e470543c76 (cherry-picked from ced355b7c49a76966fe4961ef93f53e470543c76) Change-Id: I8eeff40b22b57cc75717f8df3e34d1fbb1388c12 Signed-off-by: Senthil Kumar G S Signed-off-by: Sudipto --- resource/csdk/connectivity/src/tcp_adapter/catcpserver.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c b/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c index 8ba7189..a98e8a1 100644 --- a/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c +++ b/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c @@ -86,6 +86,11 @@ static ca_thread_pool_t g_threadPool = NULL; /** + * Thread task id. + */ +static uint32_t g_taskId = 0; + +/** * Mutex to synchronize device object list. */ static oc_mutex g_mutexObjectList = NULL; @@ -1200,7 +1205,7 @@ CAResult_t CATCPStartServer(const ca_thread_pool_t threadPool) CAResult_t res = CA_STATUS_OK; #ifndef __TIZENRT__ - res = ca_thread_pool_add_task(g_threadPool, CAReceiveHandler, NULL, NULL); + res = ca_thread_pool_add_task(g_threadPool, CAReceiveHandler, NULL, &g_taskId); #else res = ca_thread_pool_add_task(g_threadPool, CAReceiveHandler, NULL, NULL, "IoT_TCPReceive", CONFIG_IOTIVITY_TCPRECEIVE_PTHREAD_STACKSIZE); @@ -1284,9 +1289,12 @@ void CATCPStopServer() #endif oc_mutex_unlock(g_mutexObjectList); +#ifndef __TIZENRT__ + ca_thread_pool_remove_task(g_threadPool, g_taskId); +#endif + CATCPDisconnectAll(); sleep(1); - OIC_LOG(INFO, TAG, "Adapter terminated successfully"); } -- 2.7.4