From 4b07a9535caa044c5dec4e5eef9505775fb18b22 Mon Sep 17 00:00:00 2001 From: Karsten Knese Date: Mon, 30 Apr 2018 11:00:07 -0700 Subject: [PATCH 1/2] Fix memory leak in node_base --- rclcpp/src/rclcpp/node_interfaces/node_base.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rclcpp/src/rclcpp/node_interfaces/node_base.cpp b/rclcpp/src/rclcpp/node_interfaces/node_base.cpp index 46aaf4be7b..0b24690243 100644 --- a/rclcpp/src/rclcpp/node_interfaces/node_base.cpp +++ b/rclcpp/src/rclcpp/node_interfaces/node_base.cpp @@ -117,7 +117,11 @@ NodeBase::NodeBase( if (ret != RCL_RET_OK) { // Finalize the interrupt guard condition. finalize_notify_guard_condition(); - + // Finalize previously allocated node arguments + if (!rcl_arguments_fini(&options.arguments) == RCL_RET_OK) { + throw_from_rcl_error(RCL_RET_ERROR, "failed to deallocate node arguments"); + } + if (ret == RCL_RET_NODE_INVALID_NAME) { rcl_reset_error(); // discard rcl_node_init error int validation_result; From d7aa8c6736e2c7de9c7890078fcb2ea34116ec86 Mon Sep 17 00:00:00 2001 From: Shane Loretz Date: Tue, 1 May 2018 09:31:53 -0700 Subject: [PATCH 2/2] Always free arguments --- .../src/rclcpp/node_interfaces/node_base.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/rclcpp/src/rclcpp/node_interfaces/node_base.cpp b/rclcpp/src/rclcpp/node_interfaces/node_base.cpp index 0b24690243..f57c807657 100644 --- a/rclcpp/src/rclcpp/node_interfaces/node_base.cpp +++ b/rclcpp/src/rclcpp/node_interfaces/node_base.cpp @@ -118,10 +118,14 @@ NodeBase::NodeBase( // Finalize the interrupt guard condition. finalize_notify_guard_condition(); // Finalize previously allocated node arguments - if (!rcl_arguments_fini(&options.arguments) == RCL_RET_OK) { - throw_from_rcl_error(RCL_RET_ERROR, "failed to deallocate node arguments"); + if (RCL_RET_OK != rcl_arguments_fini(&options.arguments)) { + // Print message because exception will be thrown later in this code block + RCUTILS_LOG_ERROR_NAMED( + "rclcpp", + "Failed to fini arguments during error handling: %s", rcl_get_error_string_safe()); + rcl_reset_error(); } - + if (ret == RCL_RET_NODE_INVALID_NAME) { rcl_reset_error(); // discard rcl_node_init error int validation_result; @@ -187,6 +191,15 @@ NodeBase::NodeBase( // Indicate the notify_guard_condition is now valid. notify_guard_condition_is_valid_ = true; + + // Finalize previously allocated node arguments + if (RCL_RET_OK != rcl_arguments_fini(&options.arguments)) { + // print message because throwing would prevent the destructor from being called + RCUTILS_LOG_ERROR_NAMED( + "rclcpp", + "Failed to fini arguments: %s", rcl_get_error_string_safe()); + rcl_reset_error(); + } } NodeBase::~NodeBase()