Repository navigation
Description
Describe the bug
Context ros2/rcl#1198 and Yaskawa-Global/motoros2#35
I believe that there is a possible memory leak in rmw_microxrcedds. In the micro-ROS rcl repo, the following lines duplicate a string and dynamically allocate the memory if RCL_MICROROS_COMPLETE_IMPL is set.
This memory which is dynamically allocated for enclave via rcutils_strdup is deallocated in other (full) rmw implementations. For example:
However it is never deallocated in rmw_microxrcedds. I believe that this is only a problem if RCL_MICROROS_COMPLETE_IMPL is set, because that is the only case where rcutils_strdup runs and sets aside dynamic memory.
I do not believe that the solution is as simple as freeing the memory in rmw_init_options_fini, because the code below copies the enclave data to static memory, and thus cannot be freed. I might be wrong about that.
To Reproduce
I do not have a MWE, but it is apparent in MotoROS2 using the debug listener script. edit - I have a MWE below
Expected behaviour
I expect the memory set aside by rcutils_strdup to be freed by the rmw, as it is with other rmws.
System information (please complete the following information):
- YRC1000
- ROS 2: I am currently using Jazzy. I believe that the problem exists on Humble as well, though.
- Version: I am currently using a fork off of 2.4.3
Please let me know if I need to provide any more information, or if I am doing something improperly. Thank you.
Activity
jimmy-mcelwain commentedon Dec 10, 2024
Here is a MWE. It's a silly program that doesn't accomplish anything other than to demonstrate this problem, but when I use valgrind I get the following results:
This suggests that it is the
rcutils_strdupinrcl_initfujitatomoya commentedon Dec 10, 2024
@jimmy-mcelwain thanks for recreating the issue here, this does seem like a problem.
pablogs9 commentedon Dec 11, 2024
I would need to check this deeper but the for of
rclthat we maintain for micro-ROS is intended to be built withRCL_MICROROS_COMPLETE_IMPLto not defined. In a sense, this flag excludes the code that micro-ROS does not need from mainstreamrcl.In any case, I wonder why
rmwis supposed to deallocate memory being allocated byrcl?gavanderhoorn commentedon Dec 11, 2024
we understand.
But seeing as
rclmaintainers suggest this is an issue with the RMW, we thought we'd report it anyway.That is also something I'm not sure about.
Perhaps @clalancette and/or @fujitatomoya can say something about that.
clalancette commentedon Dec 11, 2024
Honestly, this seems like a bug in
rcl(or, at least, in the expectations betweenrclandrmw). I agree that it doesn't make sense for thercllayer to allocate it, and thermwlayer to free it.To fix it, we have to either:
strndupinrcl_init, move thefreeinto __cleanup_context, and remove thefreefrom all of the RMW implementations, orrcl_initand intormw_init, and leave thefreein the RMW.I would actually probably go for the latter; while it is annoying to have to duplicate that code among the various RMWs, I think it also makes more sense since this code is manipulating the
rmw_init_optionsstructure. But I don't have a strong opinion either way.fujitatomoya commentedon Dec 11, 2024
yeah, thanks for pointing this out, i focused on memory leak issue. this is not good.
the thing is that some data of
rmw_init_options_tneeds to be updated viarcl_initbeforermw_init, and a part of those isenclavewhich needs to be copied tormw_init_options_t.i am also inclined to this option, it is important to keep the clear responsibility for memory management for maintenance.
@clalancette
rmw_initrequiresrmw_init_options_t, so probably we need to havermw_init(free)_options_enclaveto allocate(deallocate) the memory in rmw implementation. what do you think?i can patch this if we are good to go. i can also give it a shot to
rmw_microxrcedds, as far as i see static memory management is done only onrmw_init_options_tstructure, i think we need to calldeallocatorbefore returning that memory slot to the static buffer. @pablogs9 @gavanderhoorn am I understanding correctly? actually this is 1st time to see this code... 😅 )pablogs9 commentedon Dec 12, 2024
@fujitatomoya that's it. I guess that checking if
init_options->enclave != nullptrand deallocating it using the default allocator would work.When this responsibility is moved to rmw implementations, the correct option is not to allocate nor deallocate this as rmw_microxrcedds does not handle SROS2 security features.
fujitatomoya commentedon Mar 5, 2025
@pablogs9 can you also close this since #318 is merged.
jimmy-mcelwain commentedon Mar 5, 2025
@fujitatomoya thank you, I appreciate it!
Are these changes expected to be backported and added to future humble and jazzy releases? Or is this change likely just for future ROS2 distributions?
fujitatomoya commentedon Mar 5, 2025
No i do not think so. rolling fix adds the new interfaces, so unlikely backported to already released distro. let me see how jazzy and humble can be fixed.