Avoid memory allocation in exit handlers 21/319221/4
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 17 Oct 2024 23:24:26 +0000 (08:24 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 18 Oct 2024 00:47:39 +0000 (09:47 +0900)
To avoid the memory allocation related to the job, this patch uses
std::shared_ptr<Job>. The memory allocation in the exit handlers causes
the crash issue. While calling Dispose(), the cynara thread sets
the nullptr to the shared queue.

Change-Id: I6668dc6fce0b3ef64c01260a6e28a332777b70ea
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/rpc-port/ac-internal.cc
src/rpc-port/cynara_thread.cc
src/rpc-port/cynara_thread.hh

index e83209b3759db56de9bc05d8852747071c8166ea..2af915c6f8c14a77b630f04d590ea2df907f25ab 100644 (file)
@@ -189,10 +189,10 @@ void AccessController::CheckAsync(int fd, std::string sender_appid,
   std::lock_guard<std::recursive_mutex> lock(GetMutex());
   auto* tmp_handle = new std::shared_ptr<AccessController>(shared_from_this());
   GMainContext* context = g_main_context_ref_thread_default();
-  Job job([=]() -> Job::Type {
+  auto job = std::make_shared<Job>([=] {
     std::lock_guard<std::recursive_mutex> job_lock(GetMutex());
     if ((*tmp_handle).use_count() == 1) {
-      delete tmp_handle;  // LCOV_EXCL_LINE
+      delete tmp_handle;           // LCOV_EXCL_LINE
       return Job::Type::Continue;  // LCOV_EXCL_LINE
     }
 
index 71712bf263db35136ecb0f3f6d150394aa10c12e..c8321b628ed3f49008266b3be9f22964bca767c8 100644 (file)
@@ -49,22 +49,25 @@ CynaraThread::~CynaraThread() {
 void CynaraThread::Dispose() {
   if (disposed_) return;
 
-  queue_.Push(Job());
+  queue_.Push(nullptr);
   thread_.join();
   disposed_ = true;
 }
 
 void CynaraThread::ThreadRun() {
   while (true) {
-    Job job = queue_.WaitAndPop();
-    if (job.Do() == Job::Type::Finish)
-      return;  // LCOV_EXCL_LINE
+    auto job = WaitAndPop();
+    if (job == nullptr || job->Do() == Job::Type::Finish) return;
   }
 }
 
-void CynaraThread::Push(Job job) {
+void CynaraThread::Push(std::shared_ptr<Job> job) {
   queue_.Push(std::move(job));
 }
 
+std::shared_ptr<Job> CynaraThread::WaitAndPop() {
+  return queue_.WaitAndPop();
+}
+
 }  // namespace internal
 }  // namespace rpc_port
index 10da11978c76bac6cc27e19d276c59d5c6047a8b..8a96b781788dae38baabe21e65feff7b4d8b90e5 100644 (file)
 #define CYNARA_THREAD_HH_
 
 #include <functional>
-#include <shared-queue.hpp>
+#include <memory>
 #include <thread>
 
+#include <shared-queue.hpp>
+
 namespace rpc_port {
 namespace internal {
 
@@ -50,17 +52,17 @@ class CynaraThread {
   CynaraThread& operator=(CynaraThread&) = delete;
   ~CynaraThread();
 
-  void ThreadRun();
-  void Push(Job job);
+  void Push(std::shared_ptr<Job> job);
   void Dispose();
 
  private:
   CynaraThread();
-  Job Pop();
+  void ThreadRun();
+  std::shared_ptr<Job> WaitAndPop();
 
   bool disposed_ = true;
   std::thread thread_;
-  mutable tizen_base::SharedQueue<Job> queue_;
+  mutable tizen_base::SharedQueue<std::shared_ptr<Job>> queue_;
 };
 
 }  // namespace internal