Skip to content

Fix deprecated rcl allocator conversion in allocator_tutorial_pmr - #817

Merged
ahcorde merged 1 commit into
ros2:rollingfrom
mini-1235:pmr-rcl-allocator
Oct 12, 2026
Merged

ahcorde merged 1 commit into
ros2:rollingfrom
mini-1235:pmr-rcl-allocator

Conversation

@mini-1235

Copy link
Copy Markdown
Contributor

Description

Fixes the deprecation warning introduced in ros2/rclcpp#3058:

rclcpp/publisher_options.hpp:137:35: warning: 'get_rcl_allocator<char, std::pmr::polymorphic_allocator<char>, nullptr>' is deprecated: Conversion of C++ allocators to C style is not valid, as the size on deallocatecan not be determined. This will be remove in future versions of ros. To suppress this warningdefine the method 'rcl_allocator_t get_rcl_allocator()' on your allocator [-Wdeprecated-declarations]
  137 |         return rclcpp::allocator::get_rcl_allocator<char>(*plain_allocator_storage_);
...
demo_nodes_cpp/src/topics/allocator_tutorial_pmr.cpp:188:26: note: in instantiation of function template specialization 'rclcpp::Node::create_publisher<...>' requested here
  188 |   auto publisher = node->create_publisher<example_interfaces::msg::UInt32>(

rclcpp/message_memory_strategy.hpp:87:41: warning: 'get_rcl_allocator<char, std::pmr::polymorphic_allocator<char>, nullptr>' is deprecated: Conversion of C++ allocators to C style is not valid, as the size on deallocatecan not be determined. This will be remove in future versions of ros. To suppress this warningdefine the method 'rcl_allocator_t get_rcl_allocator()' on your allocator [-Wdeprecated-declarations]
   87 |         rcutils_allocator_ = allocator::get_rcl_allocator<char,
...
demo_nodes_cpp/src/topics/allocator_tutorial_pmr.cpp:193:29: note: in instantiation of function template specialization 'std::make_shared<rclcpp::message_memory_strategy::MessageMemoryStrategy<...>>' requested here
  193 |   auto msg_mem_strat = std::make_shared<

rclcpp/subscription_options.hpp:200:35: warning: 'get_rcl_allocator<char, std::pmr::polymorphic_allocator<char>, nullptr>' is deprecated: Conversion of C++ allocators to C style is not valid, as the size on deallocatecan not be determined. This will be remove in future versions of ros. To suppress this warningdefine the method 'rcl_allocator_t get_rcl_allocator()' on your allocator [-Wdeprecated-declarations]
  200 |         return rclcpp::allocator::get_rcl_allocator<char>(*plain_allocator_storage_);
...
demo_nodes_cpp/src/topics/allocator_tutorial_pmr.cpp:196:27: note: in instantiation of function template specialization 'rclcpp::Node::create_subscription<...>' requested here
  196 |   auto subscriber = node->create_subscription<example_interfaces::msg::UInt32>(

rclcpp/allocator/allocator_common.hpp:123:3: note: 'get_rcl_allocator<char, std::pmr::polymorphic_allocator<char>, nullptr>' has been explicitly marked deprecated here
  123 | [[deprecated("Conversion of C++ allocators to C style is not valid, as the size on deallocate"

3 warnings generated.

allocator_tutorial_pmr uses a plain std::pmr::polymorphic_allocator, so rclcpp falls back to its deprecated C++-to-rcl allocator conversion. That conversion is not valid for std::pmr: rcl's deallocate does not pass the block size, but std::pmr::memory_resource::deallocate requires it (see ros2/rclcpp#3058).

This PR adds PmrAllocator<T>, a thin subclass of std::pmr::polymorphic_allocator<T> that provides get_rcl_allocator(). rclcpp uses this function instead of the deprecated fallback. The approach is the same as ros2/realtime_support#139 (TLSF), adapted to std::pmr:

  • rcl allocations still go through the tutorial's custom memory resource.
  • Each block stores its size in a small header (alignof(std::max_align_t) bytes), so deallocate is called with the real size and alignment.
  • rcl calls the callbacks from C, so they are noexcept and return nullptr on failure or size overflow, like malloc/calloc/realloc. reallocate leaves the original block untouched on failure, and zero_allocate zeroes the memory.

Tested on macOS (arm64) against current Rolling rclcpp, running ros2 run demo_nodes_cpp allocator_tutorial [intra] for about 5 s and stopping it with Ctrl-C:

Before After
Deprecation warnings at build 3 (publisher_options.hpp, subscription_options.hpp, message_memory_strategy.hpp) 0
Normal mode segfault on shutdown, no counts printed exits 0, prints counts
Intra mode segfault on shutdown, no counts printed exits 0, prints counts

Is this user-facing behavior change?

The demo no longer emits deprecation warnings at build time, and it exits cleanly on Ctrl-C instead of crashing. The tutorial's code listings show std::pmr::polymorphic_allocator<void> and would need a matching docs update to PmrAllocator<void>.

Did you use Generative AI?

Yes. This PR description was AI-generated with Claude Code (Claude Opus 5.5). The PmrAllocator code in this PR was also written with Claude Code (Claude Opus 5.5).

Additional Information

…orial

rclcpp's fallback C++-to-rcl allocator conversion is deprecated because
rcl's deallocate does not pass the block size, which
std::pmr::memory_resource::deallocate requires. Add a thin
polymorphic_allocator subclass whose get_rcl_allocator() stores each
block's size in a header, so rcl allocations still go through the custom
memory resource and are freed with the correct size.

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

ahcorde commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Pulls: #817
Gist: https://gist.githubusercontent.com/ahcorde/f90e9d2bcef375df1c593739f07be3ac/raw/f86bea492d1c5e2a6b515379fe1f646d1802c748/ros2.repos
BUILD args: --packages-above-and-dependencies demo_nodes_cpp
TEST args: --packages-above demo_nodes_cpp
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/20738

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

@ahcorde
ahcorde merged commit 5c61395 into ros2:rolling Oct 12, 2026
3 checks passed
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.

2 participants