roscpp overview: Initialization and Shutdown | Basics | Advanced: Traits [ROS C Turtle] | Advanced: Custom Allocators [ROS C Turtle] | Advanced: Serialization and Adapting Types [ROS C Turtle] | Publishers and Subscribers | Services | Parameter Server | Timers (Periodic Callbacks) | NodeHandles | Callbacks and Spinning | Logging | Names and Node Information | Time | Exceptions | Compilation Options | Advanced: Internals | tf/Overview | tf/Tutorials | C++ Style Guide

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.

Wiki: roscpp/Overview/MessagesCustomAllocators (last edited 2020-07-01 13:53:24 by Martin Pecka)