Skip to content

rclcpp does not support zero-copy intra-process communication for shared_ptr or std::move raw messages #2872

@Yuchen966

Description

@Yuchen966

Generated by Generative AI

No response

Operating System:

Ubuntu 22

ROS version or commit hash:

humble

RMW implementation (if applicable):

No response

RMW Configuration (if applicable):

No response

Client library (if applicable):

No response

'ros2 doctor --report' output

No response

Steps to reproduce issue

  1. Enable intra-process communication in a node:

rclcpp::NodeOptions().use_intra_process_comms(true)

  1. Create a publisher:

auto pub = node->create_publisher<std_msgs::msg::Int32>("topic", 10);

  1. Create a subscriber that accepts shared_ptr or shared ownership.

Publish a message using a shared_ptr, possibly moved:

    auto msg = std::make_shared<std_msgs::msg::Int32>();
    pub->publish(std::move(msg));
  1. Observe whether the subscriber receives the same memory address as the published message.

Additionally, inspect the test function in rclcpp/test/rclcpp/test_intra_process_manager.cpp (lines 531–582).
The comment says it publishes a shared_ptr message, but the actual code only publishes std::unique_ptr.

Expected behavior

Publishing a shared_ptr (or std::move(shared_ptr)) should avoid deep copying if intra-process communication is enabled, and all subscribers use shared ownership.

Actual behavior

  • In rclcpp, publishing a shared_ptr message always results in a deep copy, even if std::move() is used.
  • The test case test_intra_process_manager.cpp incorrectly claims that the share_ptr message memory address is the same in intra processing communication, but the code publishes std::unique_ptr, so the intended case is not tested at all.

Additional information

Tutorial Intra-Process Communication documents that only std::unique_ptr can enable zero-copy communication, which is correct.

But this contradicts the design document, which implies that shared_ptr with same ownership can also be passed without copying.

It is theoretically possible to support zero-copy for shared_ptr in safe cases (e.g., use_count() == 1), or provide an opt-in behavior to avoid unnecessary copies.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions