From d7804e1b3fd9676d302ec72f02c49ba04cbed5e6 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 25 Oct 2021 17:07:23 -0300 Subject: [PATCH] [service] Don't use a weak_ptr to avoid leaking (#1668) (#1669) Signed-off-by: Ivan Santiago Paunovic (cherry picked from commit d488535f366f9f59d3f72f6e15d1b5258c7d63c6) # Conflicts: # rclcpp/include/rclcpp/service.hpp Co-authored-by: Ivan Santiago Paunovic --- rclcpp/include/rclcpp/service.hpp | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/rclcpp/include/rclcpp/service.hpp b/rclcpp/include/rclcpp/service.hpp index 509efe8..bec8319 100644 --- a/rclcpp/include/rclcpp/service.hpp +++ b/rclcpp/include/rclcpp/service.hpp @@ -177,25 +177,16 @@ public: using rosidl_typesupport_cpp::get_service_type_support_handle; auto service_type_support_handle = get_service_type_support_handle(); - std::weak_ptr weak_node_handle(node_handle_); // rcl does the static memory allocation here service_handle_ = std::shared_ptr( - new rcl_service_t, [weak_node_handle, service_name](rcl_service_t * service) + new rcl_service_t, [handle = node_handle_](rcl_service_t * service) { - auto handle = weak_node_handle.lock(); - if (handle) { - if (rcl_service_fini(service, handle.get()) != RCL_RET_OK) { - RCLCPP_ERROR( - rclcpp::get_node_logger(handle.get()).get_child("rclcpp"), - "Error in destruction of rcl service handle: %s", - rcl_get_error_string().str); - rcl_reset_error(); - } - } else { - RCLCPP_ERROR_STREAM( - rclcpp::get_logger("rclcpp"), - "Error in destruction of rcl service handle " << service_name << - ": the Node Handle was destructed too early. You will leak memory"); + if (rcl_service_fini(service, handle.get()) != RCL_RET_OK) { + RCLCPP_ERROR( + rclcpp::get_node_logger(handle.get()).get_child("rclcpp"), + "Error in destruction of rcl service handle: %s", + rcl_get_error_string().str); + rcl_reset_error(); } delete service; });