Fix resource leak 68/266868/1
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 22 Nov 2021 02:23:25 +0000 (11:23 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 22 Nov 2021 02:23:25 +0000 (11:23 +0900)
Change-Id: I002aaf32928c912cbd41715b6a6415eee5e0fcd9
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
idlc/gen_cion/cpp_cion_proxy_body_gen_cb.h
idlc/gen_cion/cpp_cion_stub_body_gen_cb.h
idlc/gen_cion/cpp_cion_stub_header_gen_cb.h

index 64b1dd3..a11f666 100644 (file)
 const char CB_DTOR[] =
 R"__cpp_cb(
 $$::~$$() {
+  if (peer_ != nullptr)
+    cion_peer_info_destroy(peer_);
+
+  if (cion_client_ != nullptr)
+    cion_client_destroy(cion_client_);
+
+  if (cion_security_ != nullptr)
+    cion_security_destroy(cion_security_);
 }
 )__cpp_cb";
 
index f6a886e..00b5b94 100644 (file)
@@ -26,39 +26,49 @@ R"__cpp_cb(
     _E("Failed to create security handle. error(%d)", ret);
     throw InvalidIOException();
   }
+  auto security_auto = std::unique_ptr<
+      std::remove_pointer<cion_security_h>::type, decltype(cion_security_destroy)*>(
+          security, cion_security_destroy);
 
-  ret = cion_server_create(&cion_server_, service_name.c_str(),
+  cion_server_h server;
+  ret = cion_server_create(&server, service_name.c_str(),
       display_name.c_str(), security);
   if (ret != CION_ERROR_NONE) {
     _E("Failed to create handle. error(%d)", ret);
     throw InvalidIOException();
   }
+  auto server_auto = std::unique_ptr<
+      std::remove_pointer<cion_server_h>::type, decltype(cion_server_destroy)*>(
+          server, cion_server_destroy);
 
-  ret = cion_server_add_connection_result_cb(cion_server_, OnConnectionResultCB,
+  ret = cion_server_add_connection_result_cb(server, OnConnectionResultCB,
       this);
   if (ret != CION_ERROR_NONE) {
     _E("Failed to cion_server_add_connection_result_cb. error(%d)", ret);
     throw InvalidIOException();
   }
 
-  ret = cion_server_add_payload_received_cb(cion_server_, OnPayloadReceivedCB,
+  ret = cion_server_add_payload_received_cb(server, OnPayloadReceivedCB,
       this);
   if (ret != CION_ERROR_NONE) {
     _E("Failed to cion_server_add_payload_received_cb. error(%d)", ret);
     throw InvalidIOException();
   }
 
-  ret = cion_server_set_data_received_cb(cion_server_, OnDataReceivedCB, this);
+  ret = cion_server_set_data_received_cb(server, OnDataReceivedCB, this);
   if (ret != CION_ERROR_NONE) {
     _E("Failed to cion_server_set_data_received_cb. error(%d)", ret);
     throw InvalidIOException();
   }
 
-  ret = cion_server_add_disconnected_cb(cion_server_, OnDisconnectedCB, this);
+  ret = cion_server_add_disconnected_cb(server, OnDisconnectedCB, this);
   if (ret != CION_ERROR_NONE) {
     _E("Failed to cion_server_add_disconnected_cb. error(%d)", ret);
     throw InvalidIOException();
   }
+
+  cion_security_ = security_auto.release();
+  cion_server_ = server_auto.release();
 }
 )__cpp_cb";
 
@@ -68,6 +78,12 @@ R"__cpp_cb(
   for (auto& i : services_) {
     i->OnTerminate();
   }
+
+  if (cion_server_ != nullptr)
+    cion_server_destroy(cion_server_);
+
+  if (cion_security_ != nullptr)
+    cion_security_destroy(cion_security_);
 }
 
 void ##::Listen(std::shared_ptr<##::ServiceBase::Factory> service_factory) {
index ab47e68..814cc68 100644 (file)
@@ -49,7 +49,8 @@ R"__cpp_cb(
 
   std::shared_ptr<ServiceBase::Factory> service_factory_;
   std::list<std::shared_ptr<ServiceBase>> services_;
-  cion_server_h cion_server_;
+  cion_security_h cion_security_ = nullptr;
+  cion_server_h cion_server_ = nullptr;
 )__cpp_cb";
 
 const char CB_SERVICE_BASE_FRONT[] =