ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

If I call pub_->publish(some_msg->image) is a copy of the image taking place?

Yes.

Is there a way to make this work without copying?

The only way to prevent a copy when publishing is to use a unique_ptr.

Even if you were to share the ownership of the message with ROS, ROS could not ensure you didn't change it while it was in use.

For us, publish may always be asynchronous, so you can't depend on the idea that ROS is "done" with the message when publish returns. That's why it either needs to take ownership (unique_ptr) or make a copy.

If I call pub_->publish(some_msg->image) is a copy of the image taking place?

Yes.

Is there a way to make this work without copying?

The only way to prevent a copy when publishing is to use a unique_ptr.

Even if you were to share the ownership of the message with ROS, ROS could not ensure you didn't change it while it was in use.

For us, publish may always be asynchronous, so you can't depend on the idea that ROS is "done" with the message when publish returns. That's why it either needs to take ownership (unique_ptr) or make a copy.

Going the other direction has a similar question and answer- how to store a received Image shared_ptr into another message.

There's no way to do this that I'm aware of. The sub message is part of the larger message.