Fix process event life cycle 89/218789/1
authorJaehyun Cho <jaehyun3.cho@samsung.com>
Fri, 11 Oct 2019 09:09:09 +0000 (18:09 +0900)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Thu, 28 Nov 2019 07:21:07 +0000 (16:21 +0900)
process event variable need to initialize before OCInit(), event need to
register after OCInit and also event variable should be freed after OCStop()

https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/586
(cherry-picked from 3a00d744cac2e026b2b7b195b8b2984fd06ca049)

Change-Id: I88ffc088a19a4a8bf7fc58c49d436d4813921a36
Signed-off-by: Jaehyun Cho <jaehyun3.cho@samsung.com>
Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp
resource/src/InProcClientWrapper.cpp
resource/src/InProcServerWrapper.cpp

index e9e481a..239cfc2 100755 (executable)
@@ -1070,6 +1070,15 @@ int main(int argc, char* argv[])
     OCSetRAInfo(&rainfo);
 #endif
 
+#ifdef WITH_PROCESS_EVENT
+    processEvent = oc_event_new();
+    if (!processEvent)
+    {
+        OIC_LOG(INFO, TAG, "oc_event_new failed!");
+        exit (EXIT_FAILURE);
+    }
+#endif
+
     OIC_LOG(DEBUG, TAG, "OCServer is starting...");
 
     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
@@ -1123,14 +1132,8 @@ int main(int argc, char* argv[])
         OIC_LOG(INFO, TAG, "Device Registration failed!");
         exit (EXIT_FAILURE);
     }
-#ifdef WITH_PROCESS_EVENT
-    processEvent = oc_event_new();
-    if (!processEvent)
-    {
-        OIC_LOG(INFO, TAG, "oc_event_new failed!");
-        exit (EXIT_FAILURE);
-    }
 
+#ifdef WITH_PROCESS_EVENT
     OCRegisterProcessEvent(processEvent);
 #endif
 
@@ -1197,6 +1200,11 @@ int main(int argc, char* argv[])
 
     OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");
 
+    if (OCStop() != OC_STACK_OK)
+    {
+        OIC_LOG(ERROR, TAG, "OCStack process error");
+    }
+
 #ifdef WITH_PROCESS_EVENT
     if (processEvent)
     {
@@ -1205,10 +1213,5 @@ int main(int argc, char* argv[])
     }
 #endif
 
-    if (OCStop() != OC_STACK_OK)
-    {
-        OIC_LOG(ERROR, TAG, "OCStack process error");
-    }
-
     return 0;
 }
index 433d840..1062ea3 100755 (executable)
@@ -64,6 +64,17 @@ namespace OC
 
         if (m_cfg.mode == ModeType::Client)
         {
+#ifdef WITH_PROCESS_EVENT
+            if (false == m_threadRun)
+            {
+                m_processEvent = oc_event_new();
+                if (!m_processEvent)
+                {
+                    OIC_LOG(INFO, TAG, "oc_event_new failed!");
+                    return OC_STACK_ERROR;
+                }
+            }
+#endif
             OCTransportFlags serverFlags =
                             static_cast<OCTransportFlags>(m_cfg.serverConnectivity & CT_MASK_FLAGS);
             OCTransportFlags clientFlags =
@@ -80,12 +91,6 @@ namespace OC
             {
                 m_threadRun = true;
 #ifdef WITH_PROCESS_EVENT
-                m_processEvent = oc_event_new();
-                if (!m_processEvent)
-                {
-                    OIC_LOG(INFO, TAG, "oc_event_new failed!");
-                    return OC_STACK_ERROR;
-                }
                 OCRegisterProcessEvent(m_processEvent);
 #endif
                 m_listeningThread = std::thread(&InProcClientWrapper::listeningFunc, this);
@@ -113,6 +118,12 @@ namespace OC
 #endif
                 m_listeningThread.join();
             }
+            OCStackResult result = OCStop();
+
+            if (OC_STACK_OK != result)
+            {
+               throw InitializeException(OC::InitException::STACK_TERMINATE_ERROR, result);
+            }
 
 #ifdef WITH_PROCESS_EVENT
             if (m_processEvent)
@@ -121,12 +132,6 @@ namespace OC
                 m_processEvent = NULL;
             }
 #endif
-            OCStackResult result = OCStop();
-
-            if (OC_STACK_OK != result)
-            {
-               throw InitializeException(OC::InitException::STACK_TERMINATE_ERROR, result);
-            }
         }
         return OC_STACK_OK;
     }
index f36a1f3..cd56218 100755 (executable)
@@ -288,6 +288,18 @@ namespace OC
                                          OC_STACK_INVALID_PARAM);
         }
 
+#ifdef WITH_PROCESS_EVENT
+        if (false == m_threadRun)
+        {
+            m_processEvent = oc_event_new();
+            if (!m_processEvent)
+            {
+                OIC_LOG(INFO, TAG, "oc_event_new failed!");
+                return OC_STACK_ERROR;
+            }
+        }
+#endif
+
         OCTransportFlags serverFlags =
                             static_cast<OCTransportFlags>(m_cfg.serverConnectivity & CT_MASK_FLAGS);
         OCTransportFlags clientFlags =
@@ -304,12 +316,6 @@ namespace OC
         {
             m_threadRun = true;
 #ifdef WITH_PROCESS_EVENT
-            m_processEvent = oc_event_new();
-            if (!m_processEvent)
-            {
-                OIC_LOG(INFO, TAG, "oc_event_new failed!");
-                return OC_STACK_ERROR;
-            }
             OCRegisterProcessEvent(m_processEvent);
 #endif
             m_processThread = std::thread(&InProcServerWrapper::processFunc, this);
@@ -333,6 +339,13 @@ namespace OC
             m_processThread.join();
         }
 
+        OCStackResult res = OCStop();
+
+        if (OC_STACK_OK != res)
+        {
+           throw InitializeException(OC::InitException::STACK_TERMINATE_ERROR, res);
+        }
+
 #ifdef WITH_PROCESS_EVENT
         if (m_processEvent)
         {
@@ -341,13 +354,6 @@ namespace OC
         }
 #endif
 
-        OCStackResult res = OCStop();
-
-        if (OC_STACK_OK != res)
-        {
-           throw InitializeException(OC::InitException::STACK_TERMINATE_ERROR, res);
-        }
-
         return OC_STACK_OK;
     }