[Documentation] [TitleIndex] [WordIndex

Custom Container Allocators [ROS 1.1+]

Messages can now use an STL allocator of your choosing to do their container memory allocation.

Simple Use

If you have an allocator you'd like to use with the containers inside a message, the ROS_DECLARE_MESSAGE_WITH_ALLOCATOR macro will let you do it easily:

   1 ROS_DECLARE_MESSAGE_WITH_ALLOCATOR(visualization_msgs::Marker, MyMarker, MyAllocator);

This declares MyMarker as a visualization_msgs::Marker that passes MyAllocator as a template argument to any container types (currently vector and string).

   1 MyMarker marker;

Just like with an STL container, you can also pass an instance of an allocator to the messages' constructor, which will pass it along to the containers:

   1 MyAllocator a;
   2 MyMarker marker2(a);
   3 ...

Compatibility with existing code

If you publish a message with a non-default allocator (default is std::allocator), any subscribers which subscribe using the default (or any other) allocator will need to serialize and deserialize the message in order to convert it from the type with one allocator to the type with another allocator. This is true even in same-node (intraprocess) communication.


2024-11-16 17:43