From 76a706db9bd1b47171170a9299ed4234c193fc92 Mon Sep 17 00:00:00 2001 From: Jacob Perron Date: Fri, 16 Oct 2020 13:42:49 -0700 Subject: [PATCH] Fix memory leak in rcl_subscription_init()/rcl_publisher_init() (#794, #834) (#832) * Fix memory leak in rcl_subscription_init()/rcl_publisher_init() (#794) * Fix memory leak in rcl_subscription_init()/rcl_publisher_init() In rcl_subscription_init(), while rmw_subscription_get_actual_qos() return failure, created rmw subscription handle isn't freed. In rcl_publisher_init(), while rmw_publisher_get_actual_qos() return failure, created rmw publisher handle isn't freed. Signed-off-by: Barry Xu * Remove codes on the cascading errors. Signed-off-by: Barry Xu * Change code style Signed-off-by: Barry Xu * Output error message to stderr Signed-off-by: Barry Xu * Remove format string This version of the macro is not available in Foxy. Signed-off-by: Jacob Perron * Print new line Signed-off-by: Jacob Perron Co-authored-by: Barry Xu --- rcl/src/rcl/publisher.c | 9 +++++++++ rcl/src/rcl/subscription.c | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/rcl/src/rcl/publisher.c b/rcl/src/rcl/publisher.c index 6aea8f6..676dc03 100644 --- a/rcl/src/rcl/publisher.c +++ b/rcl/src/rcl/publisher.c @@ -197,6 +197,15 @@ rcl_publisher_init( goto cleanup; fail: if (publisher->impl) { + if (publisher->impl->rmw_handle) { + rmw_ret_t rmw_fail_ret = rmw_destroy_publisher( + rcl_node_get_rmw_handle(node), publisher->impl->rmw_handle); + if (RMW_RET_OK != rmw_fail_ret) { + RCUTILS_SAFE_FWRITE_TO_STDERR(rmw_get_error_string().str); + RCUTILS_SAFE_FWRITE_TO_STDERR("\n"); + } + } + allocator->deallocate(publisher->impl, allocator->state); publisher->impl = NULL; } diff --git a/rcl/src/rcl/subscription.c b/rcl/src/rcl/subscription.c index 2e3b37d..6a4af49 100644 --- a/rcl/src/rcl/subscription.c +++ b/rcl/src/rcl/subscription.c @@ -193,6 +193,15 @@ rcl_subscription_init( goto cleanup; fail: if (subscription->impl) { + if (subscription->impl->rmw_handle) { + rmw_ret_t rmw_fail_ret = rmw_destroy_subscription( + rcl_node_get_rmw_handle(node), subscription->impl->rmw_handle); + if (RMW_RET_OK != rmw_fail_ret) { + RCUTILS_SAFE_FWRITE_TO_STDERR(rmw_get_error_string().str); + RCUTILS_SAFE_FWRITE_TO_STDERR("\n"); + } + } + allocator->deallocate(subscription->impl, allocator->state); } ret = fail_ret;