[CONPRO-1476] Fix memory leak in TCP server.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / tcp_adapter / catcpserver.c
index 8ba7189..a98e8a1 100644 (file)
 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");
 }