From ef171a45c43e640fcb97fccc6367f01d39c3ae96 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 22 Nov 2021 11:23:25 +0900 Subject: [PATCH] Fix resource leak Change-Id: I002aaf32928c912cbd41715b6a6415eee5e0fcd9 Signed-off-by: Hwankyu Jhun --- idlc/gen_cion/cpp_cion_proxy_body_gen_cb.h | 8 ++++++++ idlc/gen_cion/cpp_cion_stub_body_gen_cb.h | 26 +++++++++++++++++++++----- idlc/gen_cion/cpp_cion_stub_header_gen_cb.h | 3 ++- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/idlc/gen_cion/cpp_cion_proxy_body_gen_cb.h b/idlc/gen_cion/cpp_cion_proxy_body_gen_cb.h index 64b1dd3..a11f666 100644 --- a/idlc/gen_cion/cpp_cion_proxy_body_gen_cb.h +++ b/idlc/gen_cion/cpp_cion_proxy_body_gen_cb.h @@ -20,6 +20,14 @@ 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"; diff --git a/idlc/gen_cion/cpp_cion_stub_body_gen_cb.h b/idlc/gen_cion/cpp_cion_stub_body_gen_cb.h index f6a886e..00b5b94 100644 --- a/idlc/gen_cion/cpp_cion_stub_body_gen_cb.h +++ b/idlc/gen_cion/cpp_cion_stub_body_gen_cb.h @@ -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::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::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) { diff --git a/idlc/gen_cion/cpp_cion_stub_header_gen_cb.h b/idlc/gen_cion/cpp_cion_stub_header_gen_cb.h index ab47e68..814cc68 100644 --- a/idlc/gen_cion/cpp_cion_stub_header_gen_cb.h +++ b/idlc/gen_cion/cpp_cion_stub_header_gen_cb.h @@ -49,7 +49,8 @@ R"__cpp_cb( std::shared_ptr service_factory_; std::list> 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[] = -- 2.7.4