Skip to content

Unable to use custom allocator with LoanedMessage #2671

@thomasmoore-torc

Description

@thomasmoore-torc

Bug report

Required Info:

  • Operating System:
    • Linux
  • Installation type:
    • From source
  • Version or commit hash:
    • iron, jazzy, rolling
  • DDS implementation:
    • Fast-RTPS
  • Client library (if applicable):
    • rclcpp

Steps to reproduce issue

The LoanedMessage class is defined with an AllocatorT template parameter which is used as the type for the message_allocator_ member variable:

template<typename MessageT, typename AllocatorT = std::allocator<void>>
class LoanedMessage
{
  using MessageAllocatorTraits = rclcpp::allocator::AllocRebind<MessageT, AllocatorT>;
  using MessageAllocator = typename MessageAllocatorTraits::allocator_type;

  MessageAllocator message_allocator_;
};

However, the constructor accepts an allocator parameter which is of type std::allocator<MessageT>:

  LoanedMessage(
    const rclcpp::PublisherBase & pub,
    MessageAllocator allocator)
  : pub_(pub),
    message_(nullptr),
    message_allocator_(std::move(allocator))
  {
  }

This prevents creating instances of LoanedMessage when using a custom allocator which is not based on std::allocator. Our recommendation to resolve this issue is to modify the constructor to use MessageAllocator as the type for the allocator parameter:

   LoanedMessage(
     const rclcpp::PublisherBase & pub,
-    std::allocator<MessageT> allocator)
+    MessageAllocator allocator)
   : pub_(pub),
     message_(nullptr),
     message_allocator_(std::move(allocator))
   {
   }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions