Fix bugs for result of dynamic analizer.
authorKIM JungYong <jyong2.kim@samsung.com>
Wed, 12 Oct 2016 10:39:22 +0000 (19:39 +0900)
committerUze Choi <uzchoi@samsung.com>
Thu, 13 Oct 2016 00:45:41 +0000 (00:45 +0000)
1. definitely losted memory was deallocated.
2. pthread creating type was modified for parameter deallocation.
3. invalid c-string copy was fixed.

Change-Id: Iab32535f164029ccd8a82d49968ce0a44e8b8bd1
Signed-off-by: KIM JungYong <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/13169
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
service/notification/src/consumer/NSConsumerCommon.c
service/notification/src/consumer/NSConsumerCommunication.c
service/notification/src/consumer/NSConsumerInternalTaskController.c
service/notification/src/consumer/NSConsumerMemoryCache.c
service/notification/src/consumer/NSConsumerQueue.c
service/notification/src/consumer/NSConsumerScheduler.c
service/notification/src/consumer/NSThread.c

index eb58f6b..7d32bd4 100644 (file)
@@ -103,6 +103,7 @@ void NSSetIsStartedConsumer(bool setValue)
     if (setValue == false)
     {
         pthread_mutex_destroy(*NSGetStackMutex());
+        NSOICFree(*NSGetStackMutex());
         *NSGetStackMutex() = NULL;
 
         NSOICFree(*NSGetConsumerId());
@@ -156,9 +157,14 @@ void NSProviderChanged(NSProvider * provider, NSProviderState response)
     data->state = response;
 
     NSConsumerThread * thread = NSThreadInit(NSProviderChangedFunc, (void *) data);
-    NS_VERIFY_NOT_NULL_V(thread);
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(thread,
+    {
+        NSRemoveProvider(retProvider);
+        NSOICFree(data);
+    });
 
     NSDestroyThreadHandle(thread);
+    NSOICFree(thread);
 }
 
 NSSyncInfoReceivedCallback * NSGetBoneNotificationSyncCb()
@@ -191,6 +197,7 @@ void NSNotificationSync(NSSyncInfo * sync)
     NS_VERIFY_NOT_NULL_V(thread);
 
     NSDestroyThreadHandle(thread);
+    NSOICFree(thread);
 }
 
 NSMessageReceivedCallback  * NSGetBoneMessagePostedCb()
@@ -227,6 +234,7 @@ void NSMessagePost(NSMessage * msg)
     NS_VERIFY_NOT_NULL_V(thread);
 
     NSDestroyThreadHandle(thread);
+    NSOICFree(thread);
 }
 
 NSTask * NSMakeTask(NSTaskType type, void * data)
@@ -270,11 +278,11 @@ NSMessage * NSCopyMessage(NSMessage * msg)
         NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(
                 newMsg->mediaContents, NULL, NSRemoveMessage(newMsg));
         newMsg->mediaContents->iconImage =
-                (char *)OICMalloc(sizeof(char)*strlen(msg->mediaContents->iconImage));
+                (char *)OICMalloc(sizeof(char)*strlen(msg->mediaContents->iconImage) + 1);
         NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(
                 newMsg->mediaContents->iconImage, NULL, NSRemoveMessage(newMsg));
         memcpy(newMsg->mediaContents->iconImage, msg->mediaContents->iconImage,
-               strlen(msg->mediaContents->iconImage));
+               strlen(msg->mediaContents->iconImage) + 1);
     }
 
     newMsg->extraInfo = NULL;
@@ -322,10 +330,10 @@ void NSRemoveConnections(NSProviderConnectionInfo * connections)
         tmp->messageHandle = NULL;
         tmp->syncHandle = NULL;
         NSOICFree(tmp->addr);
-        tmp = tmp->next;
+        NSProviderConnectionInfo * next = tmp->next;
+        NSOICFree(tmp);
+        tmp = next;
     }
-
-    NSOICFree(connections);
 }
 
 NSProviderConnectionInfo * NSCreateProviderConnections(OCDevAddr * inAddr)
@@ -527,7 +535,7 @@ void NSRemoveProvider_internal(void * data)
 {
     NS_VERIFY_NOT_NULL_V(data);
 
-    NSProvider_internal * prov = data;
+    NSProvider_internal * prov = (NSProvider_internal *) data;
 
     NSOICFree(prov->messageUri);
     NSOICFree(prov->syncUri);
@@ -556,7 +564,7 @@ OCStackResult NSInvokeRequest(OCDoHandle * handle,
     int mutexRet = pthread_mutex_lock(*(NSGetStackMutex()));
     NS_VERIFY_NOT_NULL(mutexRet != 0 ? NULL : (void *)1, OC_STACK_ERROR);
 
-    OCCallbackData cbdata = { 0, };
+    OCCallbackData cbdata = { NULL, NULL, NULL };
 
     cbdata.cb = callbackFunc;
     cbdata.context = callbackData;
index b4e0d8a..8340d3b 100644 (file)
@@ -457,10 +457,12 @@ NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse)
     NS_LOG(DEBUG, "get state");
     int64_t state = 0;
     getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_STATE, & state);
-    NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL,
+            NULL, NSOICFree(pId));
 
     NS_LOG(DEBUG, "create NSSyncInfo");
     NSSyncInfo * retSync = NSCreateSyncInfo_consumer(id, pId, (NSSyncType)state);
+    NSOICFree(pId);
     NS_VERIFY_NOT_NULL(retSync, NULL);
 
     NS_LOG_V(DEBUG, "Sync ID : %lld", (long long int)retSync->messageId);
@@ -558,11 +560,12 @@ void NSConsumerCommunicationTaskProcessing(NSTask * task)
         NS_VERIFY_NOT_NULL_V(task->taskData);
         NS_LOG(DEBUG, "Request Subscribe");
         NSResult ret = NSConsumerSubscribeProvider((NSProvider *)task->taskData);
+        NSRemoveProvider_internal((void *) task->taskData);
         NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *)1 : NULL);
     }
     else if (task->taskType == TASK_SEND_SYNCINFO)
     {
-        NS_VERIFY_NOT_NULL_V(task->taskData);
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(task->taskData, NSOICFree(task));
         NSSyncInfo_internal * syncInfo = (NSSyncInfo_internal *)task->taskData;
         NSProviderConnectionInfo * info = syncInfo->connection;
 
@@ -583,6 +586,11 @@ void NSConsumerCommunicationTaskProcessing(NSTask * task)
     else if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL)
     {
         NSProvider_internal * provider = (NSProvider_internal *)task->taskData;
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(provider,
+        {
+            NSRemoveProvider_internal(provider);
+            NSOICFree(task);
+        });
 
         NSProviderConnectionInfo * connections = provider->connection;
         while(connections)
@@ -609,13 +617,28 @@ void NSConsumerCommunicationTaskProcessing(NSTask * task)
     else if (task->taskType == TASK_CONSUMER_REQ_TOPIC_LIST)
     {
         NSProvider_internal * provider = NSCopyProvider_internal(task->taskData);
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(provider,
+        {
+            NSRemoveProvider_internal((void *) task->taskData);
+            NSOICFree(task);
+        });
         NSRemoveProvider_internal((NSProvider_internal *)task->taskData);
 
         NSProviderConnectionInfo * connections = provider->connection;
-        NS_VERIFY_NOT_NULL_V(connections);
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(connections,
+        {
+            NSRemoveProvider_internal((void *) provider);
+            NSRemoveProvider_internal((void *) task->taskData);
+            NSOICFree(task);
+        });
 
         char * topicUri = OICStrdup(provider->topicUri);
-        NS_VERIFY_NOT_NULL_V(topicUri);
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(topicUri,
+        {
+            NSRemoveProvider_internal((void *) provider);
+            NSRemoveProvider_internal((void *) task->taskData);
+            NSOICFree(task);
+        });
 
         OCConnectivityType type = CT_DEFAULT;
         if (connections->addr->adapter == OC_ADAPTER_TCP)
@@ -629,14 +652,23 @@ void NSConsumerCommunicationTaskProcessing(NSTask * task)
 
         NS_LOG(DEBUG, "get topic query");
         char * query = NSMakeRequestUriWithConsumerId(topicUri);
-
-        NS_VERIFY_NOT_NULL_V(query);
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(query,
+        {
+            NSRemoveProvider_internal((void *) provider);
+            NSRemoveProvider_internal((void *) task->taskData);
+            NSOICFree(task);
+        });
         NS_LOG_V(DEBUG, "topic query : %s", query);
 
         OCStackResult ret = NSInvokeRequest(NULL, OC_REST_GET, connections->addr,
                                 query, NULL, NSIntrospectTopic, (void *) provider,
                                 NSRemoveProvider_internal, type);
-        NS_VERIFY_STACK_SUCCESS_V(NSOCResultToSuccess(ret));
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(NSOCResultToSuccess(ret) == true ? (void *) 1 : NULL,
+        {
+            NSRemoveProvider_internal((void *) provider);
+            NSRemoveProvider_internal((void *) task->taskData);
+            NSOICFree(task);
+        });
 
         NSOICFree(query);
         NSOICFree(topicUri);
@@ -644,12 +676,25 @@ void NSConsumerCommunicationTaskProcessing(NSTask * task)
     else if (task->taskType == TASK_CONSUMER_SELECT_TOPIC_LIST)
     {
         NSProvider_internal * provider = (NSProvider_internal *)task->taskData;
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(provider,
+        {
+            NSRemoveProvider_internal(provider);
+            NSOICFree(task);
+        });
 
         NSProviderConnectionInfo * connections = provider->connection;
-        NS_VERIFY_NOT_NULL_V(connections);
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(connections,
+        {
+            NSRemoveProvider_internal(provider);
+            NSOICFree(task);
+        });
 
         OCRepPayload * payload = OCRepPayloadCreate();
-        NS_VERIFY_NOT_NULL_V(payload);
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(payload,
+        {
+            NSRemoveProvider_internal(provider);
+            NSOICFree(task);
+        });
 
         NSTopicLL * topicLL = provider->topicLL;
         NSTopicLL * iter = topicLL;
@@ -673,7 +718,12 @@ void NSConsumerCommunicationTaskProcessing(NSTask * task)
         if (topicLLSize > 0)
         {
             topicPayload = (OCRepPayload **) OICMalloc(sizeof(OCRepPayload *)*topicLLSize);
-            NS_VERIFY_NOT_NULL_V(topicPayload);
+            NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(topicPayload,
+            {
+                OCRepPayloadDestroy(payload);
+                NSRemoveProvider_internal(provider);
+                NSOICFree(task);
+            });
 
             while (iter || iterSize < topicLLSize)
             {
@@ -698,7 +748,12 @@ void NSConsumerCommunicationTaskProcessing(NSTask * task)
         }
 
         char * topicUri = OICStrdup(provider->topicUri);
-        NS_VERIFY_NOT_NULL_V(topicUri);
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(topicUri,
+        {
+            OCRepPayloadDestroy(payload);
+            NSRemoveProvider_internal(provider);
+            NSOICFree(task);
+        });
 
         OCConnectivityType type = CT_DEFAULT;
         if (connections->addr->adapter == OC_ADAPTER_TCP)
@@ -713,13 +768,26 @@ void NSConsumerCommunicationTaskProcessing(NSTask * task)
         NS_LOG(DEBUG, "get topic query");
         char * query = NULL;
         query = NSMakeRequestUriWithConsumerId(topicUri);
-        NS_VERIFY_NOT_NULL_V(query);
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(query,
+        {
+            NSOICFree(topicUri);
+            OCRepPayloadDestroy(payload);
+            NSRemoveProvider_internal(provider);
+            NSOICFree(task);
+        });
         NS_LOG_V(DEBUG, "topic query : %s", query);
 
         OCStackResult ret = NSInvokeRequest(NULL, OC_REST_POST, connections->addr,
                                 query, (OCPayload*)payload, NSConsumerCheckPostResult,
                                 NULL, NULL, type);
-        NS_VERIFY_STACK_SUCCESS_V(NSOCResultToSuccess(ret));
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(
+                NSOCResultToSuccess(ret) == true ? (void *) 1 : NULL,
+        {
+            NSOICFree(query);
+            NSOICFree(topicUri);
+            NSRemoveProvider_internal(provider);
+            NSOICFree(task);
+        });
 
         NSRemoveProvider_internal(provider);
         NSOICFree(query);
@@ -818,7 +886,7 @@ OCStackApplicationResult NSIntrospectTopic(
 
     NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
     NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result),
-                            OC_STACK_KEEP_TRANSACTION);
+                            OC_STACK_KEEP_TRANSACTION)
 
     NS_LOG_V(DEBUG, "GET response income : %s:%d",
             clientResponse->devAddr.addr, clientResponse->devAddr.port);
@@ -834,7 +902,11 @@ OCStackApplicationResult NSIntrospectTopic(
     NSTopicLL * newTopicLL = NSGetTopicLL(clientResponse);
 
     NSProvider_internal * provider = NSCopyProvider_internal((NSProvider_internal *) ctx);
+    NS_VERIFY_NOT_NULL(provider, OC_STACK_KEEP_TRANSACTION);
+    NSRemoveTopicLL(provider->topicLL);
     provider->topicLL = NSCopyTopicLL(newTopicLL);
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(provider->topicLL, OC_STACK_KEEP_TRANSACTION,
+                          NSRemoveProvider_internal((void *) provider));
 
     NS_LOG(DEBUG, "build NSTask");
     NSTask * task = NSMakeTask(TASK_CONSUMER_RECV_TOPIC_LIST, (void *) provider);
index 0775637..fafe0c6 100644 (file)
@@ -80,7 +80,7 @@ void NSDestroyInternalCachedList()
 
     NSDestroyMessageStateList();
     pthread_mutex_destroy(*NSGetMessageListMutex());
-    *NSGetMessageListMutex() = NULL;
+    NSOICFree(*NSGetMessageListMutex());
 }
 
 NSProvider_internal * NSProviderCacheFind(const char * providerId)
@@ -149,6 +149,8 @@ NSResult NSProviderCacheUpdate(NSProvider_internal * provider)
     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
             NS_ERROR, NSOICFree(obj));
 
+    NSOICFree(obj);
+
     return NS_OK;
 }
 
@@ -165,18 +167,22 @@ void NSCancelAllSubscription()
         NSSetProviderCacheList(ProviderCache);
     }
 
-    NSCacheElement * obj = NULL;
-    while ((obj = NSPopProviderCacheList(ProviderCache)))
+    NSCacheElement * obj = NSPopProviderCacheList(ProviderCache);
+    while (obj)
     {
         NS_LOG(DEBUG, "build NSTask");
         NSProvider * prov = NSCopyProvider((NSProvider_internal *) obj->data);
         NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(prov,
-                    NSRemoveProvider_internal((NSProvider_internal *) obj->data));
+                    NSRemoveProvider_internal((void *) obj->data));
 
         NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, prov);
         NS_VERIFY_NOT_NULL_V(task);
 
         NSConsumerPushEvent(task);
+        NSRemoveProvider_internal((void *) obj->data);
+        NSOICFree(obj);
+
+        obj = NSPopProviderCacheList(ProviderCache);
     }
 }
 
@@ -239,7 +245,8 @@ void NSConsumerHandleProviderDiscovered(NSProvider_internal * provider)
     }
 
     NSResult ret = NSProviderCacheUpdate(provider);
-    NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(ret == NS_OK ? (void *) 1 : NULL,
+            NSRemoveProvider_internal(providerCacheData));
 
     if (isAdded == false)
     {
@@ -254,16 +261,17 @@ void NSConsumerHandleProviderDiscovered(NSProvider_internal * provider)
     if (provider->accessPolicy == NS_SELECTION_CONSUMER && isSubscribing == false)
     {
         NS_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user");
-        NSProvider * providerForCb = NSCopyProvider(provider);
-        NSProviderChanged(providerForCb, NS_DISCOVERED);
-        NSRemoveProvider(providerForCb);
+        NSProvider * prov = NSCopyProvider(provider);
+        NSProviderChanged(prov, NS_DISCOVERED);
+        NSRemoveProvider(prov);
     }
     else
     {
         NS_LOG(DEBUG, "accepter is NS_ACCEPTER_PROVIDER, request subscribe");
-        NSProvider_internal * subProvider = NSCopyProvider_internal(provider);
+        NSProvider * subProvider = NSCopyProvider(provider);
         NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) subProvider);
-        NS_VERIFY_NOT_NULL_V(task);
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(task,
+                NSRemoveProvider_internal(providerCacheData));
 
         NSConsumerPushEvent(task);
     }
@@ -282,8 +290,9 @@ void NSConsumerHandleProviderDeleted(NSProvider_internal * provider)
     NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *)1 : NULL);
 
     NS_LOG_V(DEBUG, "Stopped Provider : %s", provider->providerId);
-    NSProvider * providerForCb = NSCopyProvider(provider);
-    NSProviderChanged(providerForCb, NS_STOPPED);
+    NSProvider * prov = NSCopyProvider(provider);
+    NSProviderChanged(prov, NS_STOPPED);
+    NSRemoveProvider(prov);
 }
 
 void NSConsumerHandleSubscribeSucceed(NSProvider_internal * provider)
@@ -341,6 +350,7 @@ void NSConsumerHandleRecvProviderChanged(NSMessage * msg)
 
     pthread_mutex_unlock(mutex);
     NSProviderChanged(prov, (NSProviderState) msg->messageId);
+    NSRemoveProvider(prov);
 }
 
 void NSConsumerHandleRecvMessage(NSMessage * msg)
@@ -376,7 +386,7 @@ void NSConsumerHandleMakeSyncInfo(NSSyncInfo * sync)
     NS_VERIFY_NOT_NULL_V (provider);
 
     NSProviderConnectionInfo * connections = NSCopyProviderConnections(provider->connection);
-    NSRemoveProvider_internal(provider);
+    NSRemoveProvider_internal((void *) provider);
     NS_VERIFY_NOT_NULL_V (connections);
 
     NSSyncInfo_internal * syncInfo = (NSSyncInfo_internal *)OICMalloc(sizeof(NSSyncInfo_internal));
@@ -430,6 +440,7 @@ void NSConsumerHandleRecvTopicLL(NSProvider_internal * provider)
     NS_LOG(DEBUG, "call back to user");
     NSProvider * prov = NSCopyProvider(provider);
     NSProviderChanged((NSProvider *) prov, (NSProviderState) NS_TOPIC);
+    NSRemoveProvider(prov);
 }
 
 void NSConsumerInternalTaskProcessing(NSTask * task)
@@ -443,7 +454,7 @@ void NSConsumerInternalTaskProcessing(NSTask * task)
         {
             NS_LOG(DEBUG, "Receive Subscribe succeed from provider.");
             NSConsumerHandleSubscribeSucceed((NSProvider_internal *)task->taskData);
-            NSRemoveProvider_internal((NSProvider_internal *)task->taskData);
+            NSRemoveProvider_internal((void *)task->taskData);
             break;
         }
         case TASK_CONSUMER_RECV_PROVIDER_CHANGED:
@@ -537,7 +548,7 @@ void NSUnlockMessageListMutex()
     pthread_mutex_unlock(*NSGetMessageListMutex());
 }
 
-NSMessageStateList * NSGetMessageStateList()
+NSMessageStateList ** NSGetMessageStateListAddr()
 {
     static NSMessageStateList * g_messageStateList = NULL;
     if (g_messageStateList == NULL)
@@ -549,7 +560,12 @@ NSMessageStateList * NSGetMessageStateList()
         g_messageStateList->tail = NULL;
     }
 
-    return g_messageStateList;
+    return & g_messageStateList;
+}
+
+NSMessageStateList * NSGetMessageStateList()
+{
+    return * NSGetMessageStateListAddr();
 }
 
 NSMessageStateLL * NSFindMessageState(uint64_t msgId)
@@ -684,4 +700,13 @@ void NSDestroyMessageStateList()
     NSGetMessageStateList()->tail = NULL;
 
     NSUnlockMessageListMutex();
+
+    pthread_mutex_t * mu = *NSGetMessageListMutex();
+    pthread_mutex_destroy(mu);
+    NSOICFree(mu);
+    *NSGetMessageListMutex() = NULL;
+
+    NSMessageStateList * list = NSGetMessageStateList();
+    NSOICFree(list);
+    *NSGetMessageStateListAddr() = NULL;
 }
index 5984beb..a5a63e5 100644 (file)
@@ -160,7 +160,7 @@ NSResult NSConsumerStorageDelete(NSCacheList * list, const char * delId)
 
             if (type == NS_CONSUMER_CACHE_PROVIDER)
             {
-                NSRemoveProvider_internal((NSProvider_internal *) del->data);
+                NSRemoveProvider_internal((void *) del->data);
             }
             NSOICFree(del);
             pthread_mutex_unlock(mutex);
@@ -314,7 +314,7 @@ NSResult NSConsumerStorageDestroy(NSCacheList * list)
         {
             next = (NSCacheElement *) iter->next;
 
-            NSRemoveProvider_internal((NSProvider_internal *) iter->data);
+            NSRemoveProvider_internal((void *) iter->data);
             NSOICFree(iter);
 
             iter = next;
index 59012d7..89495e7 100644 (file)
@@ -43,14 +43,11 @@ void NSDestroyQueue(NSConsumerQueue * queue)
     NSConsumerQueueObject * node = NSPopQueue(queue);
     while(node)
     {
-        NSConsumerQueueObject * next = (NSConsumerQueueObject *)node->next;
         NSOICFree(node->data);
         NSOICFree(node);
 
-        node = next;
+        node = NSPopQueue(queue);
     }
-
-    NSOICFree(queue);
 }
 
 bool NSPushConsumerQueue(NSConsumerQueue * queue, NSConsumerQueueObject * object)
index 13a33bb..d7552e4 100644 (file)
@@ -88,16 +88,16 @@ NSResult NSConsumerMessageHandlerInit()
     ret = NSConsumerSystemInit();
     NS_VERIFY_NOT_NULL(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR);
 
-    NS_LOG(DEBUG, "queue thread init");
-    handle = NSThreadInit(NSConsumerMsgHandleThreadFunc, NULL);
-    NS_VERIFY_NOT_NULL(handle, NS_ERROR);
-    NSSetMsgHandleThreadHandle(handle);
-
     NS_LOG(DEBUG, "create queue");
     queue = NSCreateQueue();
     NS_VERIFY_NOT_NULL(queue, NS_ERROR);
     NSSetMsgHandleQueue(queue);
 
+    NS_LOG(DEBUG, "queue thread init");
+    handle = NSThreadInit(NSConsumerMsgHandleThreadFunc, NULL);
+    NS_VERIFY_NOT_NULL(handle, NS_ERROR);
+    NSSetMsgHandleThreadHandle(handle);
+
     return NS_OK;
 }
 
@@ -107,6 +107,7 @@ NSResult NSConsumerPushEvent(NSTask * task)
     NS_VERIFY_NOT_NULL(thread, NS_ERROR);
 
     NSDestroyThreadHandle(thread);
+    NSOICFree(thread);
 
     return NS_OK;
 }
@@ -117,10 +118,12 @@ void NSConsumerMessageHandlerExit()
     NSConsumerListenerTermiate();
     NSCancelAllSubscription();
 
-    NSThreadStop(*(NSGetMsgHandleThreadHandle()));
+    NSConsumerThread * thread = *(NSGetMsgHandleThreadHandle());
+    NSThreadStop(thread);
     NSSetMsgHandleThreadHandle(NULL);
 
-    NSDestroyQueue(*(NSGetMsgHandleQueue()));
+    NSConsumerQueue * queue = *(NSGetMsgHandleQueue());
+    NSDestroyQueue(queue);
     NSSetMsgHandleQueue(NULL);
 
     NSDestroyInternalCachedList();
@@ -163,6 +166,7 @@ void * NSConsumerMsgHandleThreadFunc(void * threadHandle)
         if (obj)
         {
             NSConsumerTaskProcessing((NSTask *)(obj->data));
+            NSOICFree(obj);
         }
 
         NSThreadUnlock(queueHandleThread);
@@ -174,8 +178,6 @@ void * NSConsumerMsgHandleThreadFunc(void * threadHandle)
 
 void * NSConsumerMsgPushThreadFunc(void * data)
 {
-    NSThreadDetach();
-
     NSConsumerQueueObject * obj = NULL;
     NSConsumerQueue * queue = NULL;
 
@@ -317,7 +319,7 @@ void NSConsumerTaskProcessing(NSTask * task)
         {
             NSTask * getTopicTask = (NSTask *)OICMalloc(sizeof(NSTask));
             NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(getTopicTask,
-                        NSRemoveProvider_internal((NSProvider_internal *) task->taskData));
+                        NSRemoveProvider_internal((void *) task->taskData));
             getTopicTask->nextTask = NULL;
             getTopicTask->taskData =
                     (void *) NSCopyProvider_internal((NSProvider_internal *) task->taskData);
index ea5ce00..5610636 100644 (file)
@@ -53,11 +53,17 @@ NSConsumerThread * NSThreadInit(NSThreadFunc func, void * data)
 
     handle->isStarted = true;
 
-    pthreadResult = pthread_create(&(handle->thread_id), NULL, func,
+    pthread_attr_t attrDetached;
+    pthread_attr_init(& attrDetached);
+    pthread_attr_setdetachstate(& attrDetached, PTHREAD_CREATE_DETACHED);
+
+    pthreadResult = pthread_create(&(handle->thread_id), & attrDetached, func,
                            (data == NULL) ? (void *) handle : (void *)data);
     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
             NULL, NSDestroyThreadHandle(handle));
 
+    pthread_attr_destroy(& attrDetached);
+
     pthread_mutex_unlock(&g_create_mutex);
 
     return handle;
@@ -93,7 +99,9 @@ void NSThreadJoin(NSConsumerThread * handle)
 
     if (handle->thread_id)
     {
-        pthread_join(handle->thread_id, NULL);
+        void * retData = NULL;
+        pthread_join(handle->thread_id, & retData);
+        NSOICFree(retData);
     }
 }
 
@@ -104,8 +112,6 @@ void NSDestroyThreadHandle(NSConsumerThread * handle)
     pthread_mutex_destroy(&(handle->mutex));
     pthread_mutexattr_destroy(&(handle->mutex_attr));
 
-    NSOICFree(handle);
-
     pthread_mutex_unlock(&g_create_mutex);
 }