From df14174f689b164f63ba38bbb2a116e4ded1b325 Mon Sep 17 00:00:00 2001 From: Jacob Perron Date: Fri, 16 Oct 2020 13:49:10 -0700 Subject: [PATCH] Fix memory leak in rcl_subscription_init()/rcl_publisher_init() (#794) (#833) * 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 redundant error formatting (#834) Follow-up to #794. rmw_get_error_string already formats the error with the file and line number. 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 cdd11ca36..be18d27ec 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); } diff --git a/rcl/src/rcl/subscription.c b/rcl/src/rcl/subscription.c index 8747f5427..108eaed6d 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;