From 014885c1cf74a454faac700444b19e17e9313312 Mon Sep 17 00:00:00 2001 From: yuanyuyuan Date: Thu, 18 Jul 2024 18:08:27 +0800 Subject: [PATCH] fix: memory leak --- .../src/detail/attachment_helpers.cpp | 2 ++ rmw_zenoh_cpp/src/rmw_init.cpp | 6 +++++ rmw_zenoh_cpp/src/rmw_zenoh.cpp | 22 ++++++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/rmw_zenoh_cpp/src/detail/attachment_helpers.cpp b/rmw_zenoh_cpp/src/detail/attachment_helpers.cpp index ce94663b..7cd76973 100644 --- a/rmw_zenoh_cpp/src/detail/attachment_helpers.cpp +++ b/rmw_zenoh_cpp/src/detail/attachment_helpers.cpp @@ -157,6 +157,8 @@ int64_t get_int64_from_attachment(const z_loaned_bytes_t *const attachment, return -1; } + z_drop(z_move(val)); + return num; } diff --git a/rmw_zenoh_cpp/src/rmw_init.cpp b/rmw_zenoh_cpp/src/rmw_init.cpp index 90ede057..d39210f1 100644 --- a/rmw_zenoh_cpp/src/rmw_init.cpp +++ b/rmw_zenoh_cpp/src/rmw_init.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "detail/guard_condition.hpp" #include "detail/identifier.hpp" @@ -185,6 +186,8 @@ rmw_ret_t rmw_init(const rmw_init_options_t *options, rmw_context_t *context) { // Initialize the zenoh session. z_open(&context->impl->session, z_move(config)); + + if (!z_session_check(&context->impl->session)) { RMW_SET_ERROR_MSG("Error setting up zenoh session"); return RMW_RET_ERROR; @@ -342,6 +345,7 @@ rmw_ret_t rmw_init(const rmw_init_options_t *options, rmw_context_t *context) { } z_drop(z_move(reply)); + z_drop(z_move(handler)); // TODO(Yadunund): Switch this to a liveliness subscriptions once the API is // available. @@ -382,6 +386,8 @@ rmw_ret_t rmw_init(const rmw_init_options_t *options, rmw_context_t *context) { return RMW_RET_ERROR; } + z_drop(z_move(keyexpr)); + undeclare_z_sub.cancel(); close_session.cancel(); destruct_guard_condition_data.cancel(); diff --git a/rmw_zenoh_cpp/src/rmw_zenoh.cpp b/rmw_zenoh_cpp/src/rmw_zenoh.cpp index 0b69771c..b941297b 100644 --- a/rmw_zenoh_cpp/src/rmw_zenoh.cpp +++ b/rmw_zenoh_cpp/src/rmw_zenoh.cpp @@ -329,6 +329,8 @@ rmw_node_t *rmw_create_node(rmw_context_t *context, const char *name, node->context = context; node->data = node_data; + z_drop(z_move(keyexpr)); + free_token.cancel(); free_node_data.cancel(); destruct_node_data.cancel(); @@ -682,6 +684,9 @@ rmw_publisher_t *rmw_create_publisher( return nullptr; } + z_drop(z_move(keyexpr)); + z_drop(z_move(liveliness_keyexpr)); + free_token.cancel(); undeclare_z_publisher_cache.cancel(); undeclare_z_publisher.cancel(); @@ -922,7 +927,12 @@ rmw_ret_t rmw_publish(const rmw_publisher_t *publisher, const void *ros_message, // allocator->deallocate(msg_bytes, allocator->state); // } // }); - // + auto free_msg_bytes = + rcpputils::make_scope_exit([&msg_bytes, allocator]() { + if (msg_bytes) { + allocator->deallocate(msg_bytes, allocator->state); + } + }); // // Get memory from SHM buffer if available. // if (publisher_data->context->impl->shm_manager.has_value() && // zc_shm_manager_check( @@ -1713,6 +1723,7 @@ rmw_ret_t __rmw_take_one(rmw_zenoh_cpp::rmw_subscription_data_t *sub_data, } *taken = true; + z_drop(z_move(slice)); return RMW_RET_OK; } @@ -1905,6 +1916,7 @@ rmw_ret_t __rmw_take_serialized(const rmw_subscription_t *subscription, memcpy(serialized_message->buffer, z_slice_data(z_loan(payload)), z_slice_len(z_loan(payload))); + z_drop(z_move(payload)); *taken = true; if (message_info != nullptr) { @@ -2242,6 +2254,8 @@ rmw_client_t *rmw_create_client( return nullptr; } + z_drop(z_move(keyexpr)); + rmw_client->data = client_data; free_token.cancel(); @@ -2498,6 +2512,7 @@ rmw_ret_t rmw_take_response(const rmw_client_t *client, auto now_ns = std::chrono::duration_cast(now); request_header->received_timestamp = now_ns.count(); + z_drop(z_move(payload)); *taken = true; return RMW_RET_OK; @@ -2810,6 +2825,8 @@ rmw_service_t *rmw_create_service( return nullptr; } + z_drop(z_move(keyexpr)); + rmw_service->data = service_data; free_rmw_service.cancel(); @@ -2971,6 +2988,7 @@ rmw_ret_t rmw_take_request(const rmw_service_t *service, return RMW_RET_ERROR; } + z_drop(z_move(payload)); *taken = true; return RMW_RET_OK; @@ -3053,6 +3071,8 @@ rmw_ret_t rmw_send_response(const rmw_service_t *service, z_query_reply(loaned_query, z_loan(service_data->keyexpr), z_move(payload), &options); + z_drop(z_move(payload)); + return RMW_RET_OK; }