Cancel threads before exiting in
authorDoug Hudson <douglas.hudson@intel.com>
Thu, 2 Oct 2014 21:10:44 +0000 (17:10 -0400)
committerDoug Hudson <douglas.hudson@intel.com>
Thu, 2 Oct 2014 21:10:44 +0000 (17:10 -0400)
csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp and ocservercoll.cpp.
If threads are not canceled before exiting program, valgrind shows lost bytes.
While not really a stack problem, it could be distracting while trying to
use valgrind to find future memory leaks in the stack.

Change-Id: I653916211d6115381fae33cf95338c83b395d6fe

csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp
csdk/stack/samples/linux/SimpleClientServer/ocservercoll.cpp

index 8748b52..06226d1 100644 (file)
@@ -143,7 +143,7 @@ void ProcessPostRequest (OCEntityHandlerRequest *ehRequest)
                 gLedInstance[gCurrLedInstance].state = 0;
                 gLedInstance[gCurrLedInstance].power = 0;
                 sprintf (respPLPost_newLed + strlen(respPLPost_newLed), "%d", gCurrLedInstance);
-                memcpy (respPLPost_newLed + strlen(respPLPost_newLed), postPLSuffix, 
+                memcpy (respPLPost_newLed + strlen(respPLPost_newLed), postPLSuffix,
                         strlen(postPLSuffix) + 1);
                 gCurrLedInstance++;
                 respPLPost_led = respPLPost_newLed;
@@ -168,7 +168,7 @@ void ProcessPostRequest (OCEntityHandlerRequest *ehRequest)
                 if (i == 0)
                 {
                     respPLPost_led = (char *)respPLGet_ledInst0;
-                } 
+                }
                 else if (i == 1)
                 {
                     respPLPost_led = (char *)respPLGet_ledInst1;
@@ -516,6 +516,12 @@ int main(int argc, char* argv[])
         sleep(2);
     }
 
+    /*
+     * Cancel the LED thread and wait for it to terminate
+     */
+    pthread_cancel(threadId);
+    pthread_join(threadId, NULL);
+
     OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
 
     if (OCStop() != OC_STACK_OK) {
index a905f4d..35caaf0 100644 (file)
@@ -225,6 +225,12 @@ int main() {
         sleep(2);
     }
 
+    /*
+     * Cancel the LED thread and wait for it to terminate
+     */
+    pthread_cancel(threadId);
+    pthread_join(threadId, NULL);
+
     OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
 
     if (OCStop() != OC_STACK_OK) {