rclcpp publish Image message field out of an shared_ptr message
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.
Did you try the aliasing constructor for a shared pointer?
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?
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.