Ssparate the registration from the constructor 71/294671/2
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 22 Jun 2023 23:55:34 +0000 (23:55 +0000)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 23 Jun 2023 00:15:38 +0000 (00:15 +0000)
To prevent segmantation fault issue, this patch separates the app com
registration from the app event constructor.
If the main thread accesses the resource of the derived class when
calling the constructor of the base class in the sub thread,
the segmentation fault issue is occurred as below:

--------------------------------------------------------------------------------
 Thread 33 (LWP 4401):
 #0  0xb5368646 in bundle_free (b=0xabfb0270)
 #1  0xb5369538 in tizen_base::Bundle::~Bundle
 #2  0xb53c9c1e in (anonymous namespace)::AppComCreate
 #3  0xb53c9df8 in aul_app_com_create_async
 #4  0xb53a1630 in aul::AppEvent::AppEvent
 #5  0xb53a17ae in (anonymous namespace)::AppEventStub::AppEventStub

 Thread 1 (LWP 4189):
 #0  0xb5398b74 in aul::AppEvent::OnAppStatusCb
 #1  0xb53c6bd8 in (anonymous namespace)::AppComConnection::Invoke
--------------------------------------------------------------------------------

Change-Id: I045046f64c1f33dcb4ada22ebbc21cffe97b86c7
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
aul/api/aul_app_event.cc
aul/app_manager/app_event.cc
aul/app_manager/app_event.hh

index 1b1e94c3d1e3089d889f3d7fc2ae427dd868a741..96b7bcfe07ddb4c5458387a1c146590df2de9f51 100644 (file)
@@ -104,6 +104,7 @@ extern "C" API int aul_app_event_create_with_appid(const char* app_id,
   AppEventStub* handle = nullptr;
   try {
     handle = new AppEventStub(app_id, launched_cb, terminated_cb, user_data);
+    handle->Listen();
   } catch (Exception& e) {
     return e.GetErrorCode();
   }
@@ -124,6 +125,7 @@ extern "C" API int aul_app_event_create(aul_app_event_launched_cb launched_cb,
   AppEventStub* handle = nullptr;
   try {
     handle = new AppEventStub(launched_cb, terminated_cb, user_data);
+    handle->Listen();
   } catch (Exception& e) {
     return e.GetErrorCode();
   }
index 375b70dd1a8bb13bbe3197d2bd89379ba68e12b9..a475188116f348116c4033fb9a5f54e22c6a3d30 100644 (file)
@@ -19,6 +19,8 @@
 
 #include <bundle_cpp.h>
 
+#include <utility>
+
 #include "aul/app_manager/app_event.hh"
 #include "aul/common/common.hh"
 #include "aul/common/exception.hh"
@@ -28,29 +30,15 @@ namespace aul {
 
 AppEvent::AppEvent(std::string app_id, AppEvent::IEvent* ev)
   : app_id_(std::move(app_id)), ev_(ev) {
-  std::string endpoint = "app_status_event:" + app_id_;
+  endpoint_ = "app_status_event:" + app_id_;
   if (getuid() >= REGULAR_UID_MIN)
-    endpoint += ":" + std::to_string(getuid());
-
-  int ret = aul_app_com_create_async(endpoint.c_str(), nullptr, OnAppStatusCb,
-      this, &handle_);
-  if (ret != AUL_R_OK) {
-    _E("aul_app_com_create(%s) is failed. error(%d)", endpoint.c_str(), ret);
-    THROW(ret);
-  }
+    endpoint_ += ":" + std::to_string(getuid());
 }
 
 AppEvent::AppEvent(AppEvent::IEvent* ev) : ev_(ev) {
-  std::string endpoint = "app_status_event";
+  endpoint_ = "app_status_event";
   if (getuid() >= REGULAR_UID_MIN)
-    endpoint += ":" + std::to_string(getuid());
-
-  int ret = aul_app_com_create_async(endpoint.c_str(), nullptr, OnAppStatusCb,
-      this, &handle_);
-  if (ret != AUL_R_OK) {
-    _E("aul_app_com_create(%s) is failed. error(%d)", endpoint.c_str(), ret);
-    THROW(ret);
-  }
+    endpoint_ += ":" + std::to_string(getuid());
 }
 
 AppEvent::~AppEvent() {
@@ -58,6 +46,15 @@ AppEvent::~AppEvent() {
     aul_app_com_leave(handle_);
 }
 
+void AppEvent::Listen() {
+  int ret = aul_app_com_create_async(endpoint_.c_str(), nullptr, OnAppStatusCb,
+      this, &handle_);
+  if (ret != AUL_R_OK) {
+    _E("aul_app_com_create(%s) is failed. error(%d)", endpoint_.c_str(), ret);
+    THROW(ret);
+  }
+}
+
 int AppEvent::OnAppStatusCb(const char* endpoint, aul_app_com_result_e res,
     bundle* envelope, void* user_data) {
   tizen_base::Bundle b(envelope);
index 84e87a288a241b8f875c1d09c6f104c8d7fb1e06..a57d28da8bd7b612600363b42ab865726285a0a6 100644 (file)
@@ -37,6 +37,8 @@ class AppEvent {
   explicit AppEvent(IEvent* ev);
   virtual ~AppEvent();
 
+  void Listen();
+
  private:
   static int OnAppStatusCb(const char* endpoint, aul_app_com_result_e res,
       bundle* envelope, void* user_data);
@@ -44,6 +46,7 @@ class AppEvent {
  private:
   std::string app_id_;
   IEvent* ev_;
+  std::string endpoint_;
   aul_app_com_connection_h handle_ = nullptr;
 };