Skip to content

fix: Use default rcl allocator if allocator is std::allocator - #3058

Merged
jmachowinski merged 2 commits into
ros2:rollingfrom
cellumation:alloc_try2
Feb 16, 2026
Merged

jmachowinski merged 2 commits into
ros2:rollingfrom
cellumation:alloc_try2

Conversation

@jmachowinski

@jmachowinski jmachowinski commented Feb 11, 2026 •

Copy link
Copy Markdown
Collaborator

This fixes a bunch of warnings if using ASAN / valgrind on newer OS versions. It also fixed a real bug, as giving the wrong size on deallocate is undefined behavior according to the C++ standard.

This version of the patch keeps the behavior for users that specified an own allocator the same and in therefore back portable.

Alternative to #3054

USED AI:
Yes, chatgpt to generate the template methods to check for the existence of a method on a class in c++17.

@fujitatomoya fujitatomoya left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@jmachowinski thanks for fixing this issue. (it's been a looooong time... 😓 )

the approach looks good to me. a couple of things.

  • you happened to miss the DCO.
  • a few deprecated warning (which are added by this PR) detected on else where.

btw, the custom allocator path is still left broken? i think this PR explicitly leaves the retyped_allocate/retyped_deallocate bugs in place for non-std::allocator users. it means users with custom allocators will still hit the same ASAN/valgrind issues. It would be worth a comment or deprecation warning on the non-default path as well?

@jmachowinski

Copy link
Copy Markdown
Collaborator Author

@fujitatomoya This is the 'tick' version of this patch. It is also a first draft version (which I should have written in the pr...) as I did not wont to polish it if the patch got turned down again.

#3054 was discussed in the last PMC meeting and was turned down for two reasons :

  • API removal without tick / tock
  • Silent breakage for users using custom allocators.

The big drawback of #3054 was that users of custom allocators would silently fall back to malloc / free for everything happening in rcl.

I am not planning on fixing the code path for custom allocators, as the plan is to remove it altogether after the next release. I will mark it as deprecated though.

This fixes a bunch of warnings if using ASAN / valgrind on newer
OS versions. It also fixed a real bug, as giving the wrong size
on deallocate is undefined behavior according to the C++ standard.

This version of the patch keeps the behavior for users that
specified an own allocator the same and in therefore back portable.

Signed-off-by: Janosch Machowinski <[email protected]>
@jmachowinski

Copy link
Copy Markdown
Collaborator Author

This commit adds the feature, that the user can now specify a method
rcl_allocator_t get_rcl_allocator() on the given allocator. This method
will be called if present, to get the rcl allocator.
If the method is not present, the code will fall back to the old
(and faulty) implementation and show a deprecation warning.

Signed-off-by: Janosch Machowinski <[email protected]>
@jmachowinski

Copy link
Copy Markdown
Collaborator Author

@jmachowinski
jmachowinski merged commit 9f79f40 into ros2:rolling Feb 16, 2026
2 checks passed
@roncapat

roncapat commented Feb 17, 2026 •

Copy link
Copy Markdown
Contributor

@jmachowinski any chance for this to be backported on Jazzy?

BTW thanks a lot for working on this

@jmachowinski

Copy link
Copy Markdown
Collaborator Author

@roncapat
#3069

@fujitatomoya

Copy link
Copy Markdown
Collaborator

@jmachowinski we need to backport this to kilted?

@fujitatomoya

Copy link
Copy Markdown
Collaborator

@Mergifyio backport kilted

@mergify

mergify Bot commented Feb 17, 2026 •

Copy link
Copy Markdown
Contributor

backport kilted

✅ Backports have been created

Details

mergify Bot pushed a commit that referenced this pull request Feb 17, 2026
* fix: Use default rcl allocator if allocator is std::allocator

This fixes a bunch of warnings if using ASAN / valgrind on newer
OS versions. It also fixed a real bug, as giving the wrong size
on deallocate is undefined behavior according to the C++ standard.

This version of the patch keeps the behavior for users that
specified an own allocator the same and in therefore back portable.

Signed-off-by: Janosch Machowinski <[email protected]>

* feat: Provide a way to suppress the deprecation warning

This commit adds the feature, that the user can now specify a method
rcl_allocator_t get_rcl_allocator() on the given allocator. This method
will be called if present, to get the rcl allocator.
If the method is not present, the code will fall back to the old
(and faulty) implementation and show a deprecation warning.

Signed-off-by: Janosch Machowinski <[email protected]>

---------

Signed-off-by: Janosch Machowinski <[email protected]>
Co-authored-by: Janosch Machowinski <[email protected]>
(cherry picked from commit 9f79f40)
bartalor pushed a commit to bartalor/rclcpp that referenced this pull request Jul 15, 2026
)

* fix: Use default rcl allocator if allocator is std::allocator

This fixes a bunch of warnings if using ASAN / valgrind on newer
OS versions. It also fixed a real bug, as giving the wrong size
on deallocate is undefined behavior according to the C++ standard.

This version of the patch keeps the behavior for users that
specified an own allocator the same and in therefore back portable.

Signed-off-by: Janosch Machowinski <[email protected]>

* feat: Provide a way to suppress the deprecation warning

This commit adds the feature, that the user can now specify a method
rcl_allocator_t get_rcl_allocator() on the given allocator. This method
will be called if present, to get the rcl allocator.
If the method is not present, the code will fall back to the old
(and faulty) implementation and show a deprecation warning.

Signed-off-by: Janosch Machowinski <[email protected]>

---------

Signed-off-by: Janosch Machowinski <[email protected]>
Co-authored-by: Janosch Machowinski <[email protected]>
mini-1235 added a commit to mini-1235/ros2_documentation that referenced this pull request Oct 12, 2026
rclcpp falls back to a deprecated C++-to-rcl allocator conversion when the
allocator has no get_rcl_allocator() member; that conversion passes a wrong
size to deallocate (see ros2/rclcpp#3058). Explain why rcl needs its own
allocator, show a std::pmr::polymorphic_allocator wrapper that provides one
by storing the block size in a header, and use it in the example main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Maurice <[email protected]>
fujitatomoya added a commit to ros2/ros2_documentation that referenced this pull request Nov 11, 2026
…#7296)

* Allocator tutorial: provide get_rcl_allocator() for the pmr allocator

rclcpp falls back to a deprecated C++-to-rcl allocator conversion when the
allocator has no get_rcl_allocator() member; that conversion passes a wrong
size to deallocate (see ros2/rclcpp#3058). Explain why rcl needs its own
allocator, show a std::pmr::polymorphic_allocator wrapper that provides one
by storing the block size in a header, and use it in the example main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Maurice <[email protected]>

* Allocator tutorial: show the full PmrAllocator implementation

Replace the elided allocate/deallocate bodies with the complete code from
demo_nodes_cpp's allocator_tutorial_pmr.cpp, including reallocate_block,
zero_allocate_block and the required includes, and drop the note about
the fallback conversion.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Maurice <[email protected]>

* Apply batched suggestions from code review

Co-authored-by: Katherine Scott <[email protected]>
Signed-off-by: Maurice Alexander Purnawan <[email protected]>

* Clarify allocator tutorial code and fix sentence lint

Signed-off-by: Maurice <[email protected]>

---------

Signed-off-by: Maurice <[email protected]>
Signed-off-by: Maurice Alexander Purnawan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Katherine Scott <[email protected]>
Co-authored-by: Tomoya Fujita <[email protected]>
fujitatomoya added a commit to ros2/ros2_documentation that referenced this pull request Nov 11, 2026
…#7296) (#7456)

* Allocator tutorial: provide get_rcl_allocator() for the pmr allocator

rclcpp falls back to a deprecated C++-to-rcl allocator conversion when the
allocator has no get_rcl_allocator() member; that conversion passes a wrong
size to deallocate (see ros2/rclcpp#3058). Explain why rcl needs its own
allocator, show a std::pmr::polymorphic_allocator wrapper that provides one
by storing the block size in a header, and use it in the example main.




* Allocator tutorial: show the full PmrAllocator implementation

Replace the elided allocate/deallocate bodies with the complete code from
demo_nodes_cpp's allocator_tutorial_pmr.cpp, including reallocate_block,
zero_allocate_block and the required includes, and drop the note about
the fallback conversion.




* Apply batched suggestions from code review




* Clarify allocator tutorial code and fix sentence lint



---------






(cherry picked from commit 2108c88)

Signed-off-by: Maurice <[email protected]>
Signed-off-by: Maurice Alexander Purnawan <[email protected]>
Co-authored-by: Maurice Alexander Purnawan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Katherine Scott <[email protected]>
Co-authored-by: Tomoya Fujita <[email protected]>
christophebedard added a commit to ros2/demos that referenced this pull request Nov 27, 2026
`rclcpp` is removing the deprecated conversion of C++ allocators to
rcl allocators, since it is not valid, so allocators other than
`std::allocator<void>` need to define `rcl_allocator_t
get_rcl_allocator()`, as recommended since
ros2/rclcpp#3058 (Lyrical). This allocator
uses `malloc()` and `free()`, so its rcl allocator can count calls and
forward them.

Signed-off-by: Christophe Bedard <[email protected]>
christophebedard added a commit that referenced this pull request Nov 28, 2026
Converting a C++ allocator to an rcl allocator with the generic
`allocator::get_rcl_allocator()` and the `retyped_*()` functions has
been deprecated since #3058
(Lyrical), since it is not valid: the size on deallocate cannot be
determined. Allocators other than `std::allocator<void>` now need to
define `rcl_allocator_t get_rcl_allocator()`, which is enforced with a
`static_assert`.

Signed-off-by: Christophe Bedard <[email protected]>
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.

4 participants