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 when adding the same key to the logger hash map multiple times #391

Merged
merged 3 commits into from
Oct 13, 2022

Conversation

jacobperron
Copy link
Member

The first time a key is added the hash map will retain a copy of the pointer and later will deallocate the memory during shutdown. Subsequent times the same key is added to the hash map, it will NOT retain a copy of the new pointer, and hence not deallocate the memory during shutdown.

This fixes the issue by checking if we're adding an entry for an existing key and if so, deallocate the key.

Previously, ASAN was reporting memory leaks: https://ci.ros2.org/view/nightly/job/nightly_linux_address_sanitizer/1287/
With this change, we no longer see the memory leak reported: https://ci.ros2.org/job/ci_linux/17486/

…ple times

The first time a key is added the hash map will retain a copy of the pointer and later will deallocate the memory during shutdown.
Subsequent times the same key is added to the hash map, it will NOT retain a copy of the new pointer, and hence not deallocate the
memory during shutdown.

This fixes the issue by checking if we're adding an entry for an existing key and if so, deallocate the key.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
Copy link
Contributor

@clalancette clalancette left a comment

Choose a reason for hiding this comment

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

Yikes, good catch.

Actually, we can improve performance here if we do this a slightly different way. What I'm thinking here is that we do the following:

  1. On entry to the function, check to see if the passed-in name is a key in the hashmap.
  2. If it is, set copy_name to name, since we know that we aren't handing ownership of it to the hashmap.
  3. If it isn't, strdup name into copy_name, since we are handing ownership to the hashmap.
  4. Run the rest of the function as-is.

That way, for the case where we are updating the level, we never bother doing the strdup/deallocate, since it is unnecessary. What do you think?

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
@jacobperron
Copy link
Member Author

Makes sense to me. I've updated the logic to avoid the extra allocation/deallocation: cf76443

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
@jacobperron
Copy link
Member Author

jacobperron commented Oct 12, 2022

ASAN: Build Status (unrelated warning)

CI (testing above rcutils):

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Windows Build Status

Copy link
Contributor

@clalancette clalancette left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me with green CI and ASAN. Thanks for the comments as well; those are really helpful.

Copy link
Collaborator

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

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

lgtm 👍

@jacobperron jacobperron merged commit 9a84764 into rolling Oct 13, 2022
@delete-merged-branch delete-merged-branch bot deleted the jacob/fix_memory_leak branch October 13, 2022 08:08
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