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

Revision history [back]

A bit of explanations could be found in this ROS Answers topic there: https://answers.ros.org/question/363135/why-is-everything-a-shared_ptr-in-ros2/

Especially this:

One thing I noticed is that shared pointers are pervasive in that if you use shared_from_thisinside the class definition (and that happens quite a bit) so the node can pass itself to members, then it would be undefined behavior until C++17 if the node wasn't created as a shared pointer; https://en.cppreference.com/w/cpp/memory/enable_shared_from_this/shared_from_this

Certainly not great to require knowing how your instance is created/stored but that's how it is.

More information could be found on this great post with explanations: https://roboticsbackend.com/write-minimal-ros2-cpp-node/, e.g.

With ROS2 you can use Cpp 14, so ditch the “new”/”delete” keywords, and use smart pointers instead.

and

Because the node is stored in a smart pointer, you don’t need to worry about de-allocating its resources.

I dig for a moment through an issue there: https://github.com/ros2/design/issues/56#issuecomment-149368132 And I learned more too about decisions about publishers and subscribers interfaces, e.g.

The idea was that we could use smart pointers to convey the semantics of a their life cycle to the user rather than having an internally reference counted object of our own. Also in ROS 2, the Publisher and Subscription objects returned to the user are held internally as a weak_ptr, therefore if the user lets the shared pointers go out of scope they effectively unadvertised or unsubscribe them.

If you can, add more resources you have found too :) I would be interested in them as well