-
Notifications
You must be signed in to change notification settings - Fork 101
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
Conversation
…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>
There was a problem hiding this 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:
- On entry to the function, check to see if the passed-in
name
is a key in the hashmap. - If it is, set
copy_name
toname
, since we know that we aren't handing ownership of it to the hashmap. - If it isn't, strdup
name
intocopy_name
, since we are handing ownership to the hashmap. - 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>
Makes sense to me. I've updated the logic to avoid the extra allocation/deallocation: cf76443 |
Signed-off-by: Jacob Perron <jacob@openrobotics.org>
There was a problem hiding this 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm 👍
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/