[service] Don't use a weak_ptr to avoid leaking (#1668) (#1669)

Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
(cherry picked from commit d488535f366f9f59d3f72f6e15d1b5258c7d63c6)

# Conflicts:
#	rclcpp/include/rclcpp/service.hpp

Co-authored-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
This commit is contained in:
mergify[bot] 2021-10-25 17:07:23 -03:00 committed by GitHub
parent e70a07d0c0
commit d7804e1b3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -177,25 +177,16 @@ public:
using rosidl_typesupport_cpp::get_service_type_support_handle; using rosidl_typesupport_cpp::get_service_type_support_handle;
auto service_type_support_handle = get_service_type_support_handle<ServiceT>(); auto service_type_support_handle = get_service_type_support_handle<ServiceT>();
std::weak_ptr<rcl_node_t> weak_node_handle(node_handle_);
// rcl does the static memory allocation here // rcl does the static memory allocation here
service_handle_ = std::shared_ptr<rcl_service_t>( service_handle_ = std::shared_ptr<rcl_service_t>(
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 (rcl_service_fini(service, handle.get()) != RCL_RET_OK) {
if (handle) { RCLCPP_ERROR(
if (rcl_service_fini(service, handle.get()) != RCL_RET_OK) { rclcpp::get_node_logger(handle.get()).get_child("rclcpp"),
RCLCPP_ERROR( "Error in destruction of rcl service handle: %s",
rclcpp::get_node_logger(handle.get()).get_child("rclcpp"), rcl_get_error_string().str);
"Error in destruction of rcl service handle: %s", rcl_reset_error();
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");
} }
delete service; delete service;
}); });