Fix aul_app_event_destroy function 28/279528/1
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 11 Aug 2022 03:42:32 +0000 (12:42 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 11 Aug 2022 03:43:12 +0000 (12:43 +0900)
If the thread releases the handle using aul_app_event_destroy(), the
main thread will have crashed while invoking the app event callback
function.
To make thread safe, the handle will be released using g_idle_add_full().

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

index d8d5e0847b69bcb26a4f6022dc3163265fbf22e7..b93af89fdc1b3645cb8eab3feedb001054fa22fb 100644 (file)
@@ -15,6 +15,9 @@
  */
 
 #include "aul/api/aul_app_event.h"
+
+#include <glib.h>
+
 #include "aul/app_manager/app_event.hh"
 #include "aul/common/api.hh"
 #include "aul/common/exception.hh"
@@ -45,17 +48,37 @@ class AppEventStub : public AppEvent,
       user_data_(user_data) {
   }
 
- void OnAppLaunched(const AppContext* context) override {
-   AppContext* ctx = const_cast<AppContext*>(context);
-   launched_cb_(static_cast<aul_app_context_h>(ctx), user_data_);
- }
+  void OnAppLaunched(const AppContext* context) override {
+    if (disposing_)
+      return;
+
+    AppContext* ctx = const_cast<AppContext*>(context);
+    launched_cb_(static_cast<aul_app_context_h>(ctx), user_data_);
+  }
+
+  void OnAppTerminated(const AppContext* context) override {
+    if (disposing_)
+      return;
 
- void OnAppTerminated(const AppContext* context) override {
-   AppContext* ctx = const_cast<AppContext*>(context);
-   terminated_cb_(static_cast<aul_app_context_h>(ctx), user_data_);
- }
+    AppContext* ctx = const_cast<AppContext*>(context);
+    terminated_cb_(static_cast<aul_app_context_h>(ctx), user_data_);
+  }
+
+  void Dispose() {
+    if (disposing_)
+      return;
+
+    disposing_ = true;
+    g_idle_add_full(G_PRIORITY_HIGH,
+    [](gpointer user_data) {
+        auto* event = static_cast<AppEventStub*>(user_data);
+        delete event;
+        return G_SOURCE_REMOVE;
+       }, this, nullptr);
+  }
 
  private:
+  bool disposing_ = false;
   aul_app_event_launched_cb launched_cb_;
   aul_app_event_terminated_cb terminated_cb_;
   void* user_data_;
@@ -81,7 +104,6 @@ extern "C" API int aul_app_event_create_with_appid(const char* app_id,
   }
 
   *app_event = static_cast<aul_app_event_h>(handle);
-
   return AUL_R_OK;
 }
 
@@ -102,7 +124,6 @@ extern "C" API int aul_app_event_create(aul_app_event_launched_cb launched_cb,
   }
 
   *app_event = static_cast<aul_app_event_h>(handle);
-
   return AUL_R_OK;
 }
 
@@ -113,7 +134,6 @@ extern "C" API int aul_app_event_destroy(aul_app_event_h app_event) {
   }
 
   auto* handle = static_cast<AppEventStub*>(app_event);
-  delete handle;
-
+  handle->Dispose();
   return AUL_R_OK;
 }