Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in node_base #467

Closed
wants to merge 1 commit into from
Closed

Conversation

Karsten1987
Copy link
Contributor

Connects to #461 (comment)

Running valgrind with the proposed fix cleans up the memory leaked in the referenced test_node.

@Karsten1987 Karsten1987 added the in review Waiting for review (Kanban column) label Apr 30, 2018
@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh oops. Maybe rcl_node_init()should always finalize the passed in rcl_node_options_t even when an error occurs? At the very least it should document when it takes ownership of the options and when it doesn.t

Copy link
Contributor Author

@Karsten1987 Karsten1987 Apr 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i actually think the rcl_node_fini functions also lacks the call for rcl_arguments_fini.
I wasn't sure whether the node options arguments are a real copy or not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem looks larger than this leak. I think adding rcl_arguments_t broke some assumptions about rcl_node_options_t.

https://github.com/ros2/rcl/blob/fdd534e19e3ee5c5551077288749053c32ddc968/rcl/src/rcl/node.c#L226-L227

rcl_arguments_t isn't trivially copyable since it's using PIMPL, so adding it to rcl_node_options_t made that not trivially copyable as well. Maybe rcl_arguments_t should become a separate parameter to rcl_node_init().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you can make a rcl_node_options_copy(src, dst) function and then make sure to use it everywhere.

Also, I'd express a slight preference to have who ever called rcl_arguments_init() (or the equivalent) also be the one to call rcl_arguments_fini(). That is to say, having rcl_node_init/fini clean up the arguments is a bit undesirable because it's hard to look at this code and see that arguments are cleaned up properly. The trade-off might be that the node has to copy the given arguments structure or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other side, one could argue that when rcl_node_fini is called the complete struct including its nested types ought to be cleaned up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's on the "other side", if the clean up is done in this method, then the node would need to make a copy to store in itself, and then the node would clean up that copy in the fini function as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made pull request ros2/rcl#231 to make rcl_node_init() deep copy the options. If that's accepted then this PR could be changed to always free the arguments.

I think making an rcl_arguments_t argument to rcl_node_init() is less error prone. Anywhere it's not passed is visible as a compiler error, while anywhere a node options structure is shallow copied is not. I didn't change that though since it would have made for a much larger PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fair point, I'd also be ok with that, but the idea behind the node options is that it prevents parameter bloat in the rcl_node_init() function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, the default node options will never have a valid arguments field, so maybe it doesn't belong in the options struct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if the arguments are a separate argument to the init function, I'd say it should be copied, however.

@Karsten1987
Copy link
Contributor Author

closing this in favor of #468

@Karsten1987 Karsten1987 closed this May 1, 2018
@Karsten1987 Karsten1987 removed the in review Waiting for review (Kanban column) label May 1, 2018
@dirk-thomas dirk-thomas deleted the Karsten1987-patch-1 branch April 4, 2019 21:44
nnmm pushed a commit to ApexAI/rclcpp that referenced this pull request Jul 9, 2022
Signed-off-by: Emerson Knapp <eknapp@amazon.com>
DensoADAS pushed a commit to DensoADAS/rclcpp that referenced this pull request Aug 5, 2022
Signed-off-by: Emerson Knapp <eknapp@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants