Ask Your Question
0

rclcpp publish Image message field out of an shared_ptr message

asked 2018-10-31 13:33:21 -0500

lucasw gravatar image

updated 2018-10-31 15:09:15 -0500

I'm converting some ros1 code that stored Images inside other messages (sometimes service requests or responses), did work internally then would publish the image out on a regular Image topic.

Now I have changed the aggregate message to a std::shared_ptr, but I think the Image messages within it are not themselves separate shared_ptrs. If I publish a sub-message the node crashes immediately(no helpful exception messages-- is ros2 run hiding those?) (update whoops forgot to create_publisher there, that's the reason for the immediate crash) . The outer message shared_ptr has no idea that some data within it needs to live on as a published message so would free it all when it can.

If I call pub_->publish(some_msg->image) is a copy of the image taking place? Is there a way to make this work without copying?

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

edit retag flag offensive close merge delete

Comments

1

Did you try the aliasing constructor for a shared pointer?

template <class Y>
shared_ptr (const shared_ptr<Y>& r, T* p) noexcept;
allenh1 gravatar imageallenh1 ( 2018-10-31 14:45:05 -0500 )edit

I'll look into that. I can make a new Image shared_ptr using the aggregate message shared_ptr in the constructor, and it does the right thing- doesn't free memory until both the message and the Image are finished with it?

lucasw gravatar imagelucasw ( 2018-10-31 15:11:51 -0500 )edit
1

the idea is that it tells the parent shared pointer that one of its members is a dependent, so that it increments the ref count. So if the parent gets scoped out, but there is still a reference to a child object, it doesn't dangle and preserves the parent until the child is also scoped out.

allenh1 gravatar imageallenh1 ( 2018-11-01 07:45:21 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-10-31 15:40:26 -0500

William gravatar image

updated 2018-10-31 15:41:16 -0500

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.

edit flag offensive delete link more

Comments

Now that I search for unique_ptr I see https://github.com/ros2/demos/blob/bo... is using it for Image, and there is some text in the CMakeLists.txt about minimizing copies.

lucasw gravatar imagelucasw ( 2018-11-01 09:09:50 -0500 )edit
1

There's also this tutorial: https://index.ros.org/doc/ros2/Intra-... and this issue is also related: https://github.com/ros2/rclcpp/pull/504

William gravatar imageWilliam ( 2018-11-01 12:32:41 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-10-31 13:33:21 -0500

Seen: 59 times

Last updated: Oct 31 '18