Fix race condition related to global variable deallocation on process termination
authorjusung son <jusung07.son@samsung.com>
Wed, 18 Dec 2024 00:29:26 +0000 (09:29 +0900)
committerjusung son <jusung07.son@samsung.com>
Wed, 18 Dec 2024 03:27:48 +0000 (12:27 +0900)
Change-Id: I1a10a755c6be1e675d874528be62bdbec5e221a0
Signed-off-by: jusung son <jusung07.son@samsung.com>
src/rpc-port/debug-port-internal.cc

index f94417e58bbdbae856ddc0f0855252e0b5ef608c..97489da002ef3bc16cc2663d640f90a2f42c2b9a 100644 (file)
@@ -123,6 +123,7 @@ class DebugPortImpl {
 };
 
 DebugPortImpl::~DebugPortImpl() {
+  _I("");
   Dispose();
 }
 
@@ -363,32 +364,50 @@ int DebugPortImpl::AppComCb(const char* endpoint, aul_app_com_result_e result,
   return 0;
 }
 
-DebugPortImpl impl;
+DebugPortImpl* impl = new DebugPortImpl();
+
+__attribute__((destructor)) static void destructor() {
+  delete impl;
+  impl = nullptr;
+}
+
 
 }  // namespace
 
 bool DebugPort::IsConnected() {
-  impl.Init();
-  return impl.IsConnected();
+  if (impl == nullptr)
+    return false;
+
+  impl->Init();
+  return impl->IsConnected();
 }
 
 void DebugPort::AddSession(std::string port_name,
                            std::shared_ptr<Port> main_port,
                            std::shared_ptr<Port> delegate_port) {
-  impl.Init();
-  return impl.AddSession(std::move(port_name), std::move(main_port),
+  if (impl == nullptr)
+    return;
+
+  impl->Init();
+  return impl->AddSession(std::move(port_name), std::move(main_port),
                          std::move(delegate_port));
 }
 
 void DebugPort::RemoveSession(int port) {
-  impl.Init();
-  impl.RemoveSession(port);
+  if (impl == nullptr)
+    return;
+
+  impl->Init();
+  impl->RemoveSession(port);
 }
 
 void DebugPort::Send(int port, bool is_read, uint32_t seq, const void* buf,
                      unsigned int size) {
-  impl.Init();
-  impl.Send(port, is_read, seq, buf, size);
+  if (impl == nullptr)
+    return;
+
+  impl->Init();
+  impl->Send(port, is_read, seq, buf, size);
 }
 
 }  // namespace internal