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

Free with the same allocator in rmw_destroy_node #355

Merged
merged 1 commit into from
Dec 1, 2021

Conversation

jacobperron
Copy link
Member

@jacobperron jacobperron commented Nov 28, 2021

Since rmw_allocate() was used to allocate memory, we should use rmw_free() to cleanup.
Otherwise, if the user provided a custom allocator to the context we will be calling deallocate with the wrong allocator.


I hit a runtime error due to this bug while I was using a custom allocator.
There may be other places in the code where we're making a similar mistake. I haven't audited the rest of the code, but I'll make sure to fix similar instances if I come across them.

This should be considered for backport to Galactic and Foxy!

Since rmw_allocate() was used to allocate memory, we should use rmw_free() to cleanup.
Otherwise, if the user provided a custom allocator to the context we will be calling deallocate with the wrong allocator.

Signed-off-by: Jacob Perron <[email protected]>
@jacobperron jacobperron added the bug Something isn't working label Nov 28, 2021
Copy link
Collaborator

@eboasson eboasson left a comment

Choose a reason for hiding this comment

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

It looks like it is the only case for name/namespace so this is clearly a fix for a problem.

However, it seems to me that using context->options.allocator instead of rmw_allocate/rmw_free would arguably be the "better" way to allocate and free memory in the RMW layer. There are only a handful of cases, so if this is indeed the case, it probably makes more sense to change it everywhere.

@jacobperron
Copy link
Member Author

I just went with a minimal change in this PR, but I agree that using the context allocator seems like the better option.
I'll look into replacing all the instances of rmw_allocate/rmw_free.

@jacobperron
Copy link
Member Author

So, it's not trivial to use the context's allocator for everything. In some places, though we have access to the context during initialization, we don't have a reference during cleanup. E.g. rmw_create_wait_set can use the context's allocator, but we don't have a reference to it in rmw_destroy_wait_set:

extern "C" rmw_wait_set_t * rmw_create_wait_set(rmw_context_t * context, size_t max_conditions)

extern "C" rmw_ret_t rmw_destroy_wait_set(rmw_wait_set_t * wait_set)

Here's a related issue upstream: ros2/rmw#260

@eboasson
Copy link
Collaborator

@jacobperron given ros2/rmw#260 , it probably makes the most sense to do your fix first. @ivanpauno do you agree?

@ivanpauno
Copy link
Member

@jacobperron given ros2/rmw#260 , it probably makes the most sense to do your fix first. @ivanpauno do you agree?

Yes, I think it makes more sense to first merge a fix here.
ros2/rmw#260 is a larger task.

@eboasson
Copy link
Collaborator

eboasson commented Dec 1, 2021

CI looks good to me, I think all the failures are unrelated:

  • Linux Build Status
  • Linux aarch64 Build Status
  • Windows Build Status
  • macOS Build Status

@ivanpauno or @hidmic agree? If so, I'll merge this PR.

@ivanpauno
Copy link
Member

CI looks good to me, I think all the failures are unrelated:

Yes, they all look unrelated.

This one looks interesting though https://ci.ros2.org/job/ci_osx/13347/testReport/junit/projectroot.src.core.ddsi/tests/CUnit_ddsi_locator_from_string_ipv6_invalid/.
I haven't seen it before, but maybe it's a flaky test in cyclonedds (?).

@ivanpauno ivanpauno merged commit 287e781 into master Dec 1, 2021
@delete-merged-branch delete-merged-branch bot deleted the jacob/fix_free_with_other_allocator branch December 1, 2021 15:55
@eboasson
Copy link
Collaborator

eboasson commented Dec 2, 2021

This one looks interesting though https://ci.ros2.org/job/ci_osx/13347/testReport/junit/projectroot.src.core.ddsi/tests/CUnit_ddsi_locator_from_string_ipv6_invalid/.
I haven't seen it before, but maybe it's a flaky test in cyclonedds (?).

Yes, that's weird. I have had some problems with those tests in the past because of platform differences in name resolution, but not for a long time. I sampled some of the nightly builds and I haven't seen it there, so I'm inclined to provisionally ascribe it to a name-lookup oddity.

jacobperron added a commit that referenced this pull request Jan 31, 2022
Since rmw_allocate() was used to allocate memory, we should use rmw_free() to cleanup.
Otherwise, if the user provided a custom allocator to the context we will be calling deallocate with the wrong allocator.

Signed-off-by: Jacob Perron <[email protected]>
jacobperron added a commit that referenced this pull request Jan 31, 2022
Since rmw_allocate() was used to allocate memory, we should use rmw_free() to cleanup.
Otherwise, if the user provided a custom allocator to the context we will be calling deallocate with the wrong allocator.

Signed-off-by: Jacob Perron <[email protected]>
jacobperron added a commit that referenced this pull request Feb 4, 2022
Since rmw_allocate() was used to allocate memory, we should use rmw_free() to cleanup.
Otherwise, if the user provided a custom allocator to the context we will be calling deallocate with the wrong allocator.

Signed-off-by: Jacob Perron <[email protected]>
jacobperron added a commit that referenced this pull request Feb 7, 2022
Since rmw_allocate() was used to allocate memory, we should use rmw_free() to cleanup.
Otherwise, if the user provided a custom allocator to the context we will be calling deallocate with the wrong allocator.

Signed-off-by: Jacob Perron <[email protected]>
clalancette pushed a commit to eboasson/rmw_cyclonedds that referenced this pull request May 18, 2022
Since rmw_allocate() was used to allocate memory, we should use rmw_free() to cleanup.
Otherwise, if the user provided a custom allocator to the context we will be calling deallocate with the wrong allocator.

Signed-off-by: Jacob Perron <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants