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

make_unique vs make_shared

asked 2022-11-23 14:47:38 -0500

jrtg gravatar image

Hello,

In the TF2 listener tutorial (c++), the buffer is defined as a std::unique_ptr whereas the listener is a std::shared_ptr.

tf_buffer_ = std::make_unique<tf2_ros::Buffer>(this->get_clock());
tf_listener_ = std::make_shared<tf2_ros::TransformListener>(*tf_buffer_);

Is this intentional? I.e. is there a specific reason for the listener to be a std::shared_ptr, or could it just as well also have been a std::unique_ptr?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2023-06-02 03:03:47 -0500

updated 2023-06-02 03:29:37 -0500

Hi,

I am not sure it matters a lot. Actually when using tf inside a node you can also not use pointers at all. You just have to initialize the constructors of buffer and listener in the member initializer list.

class MyNode : public rclcpp::Node
{
public:
    MyNode(rclcpp::NodeOptions options)
        : Node("the_name", options), tf_buffer(get_clock()), tf_listener(tf_buffer)
    {
    // constructor
    }

private:

// TF 2 stuff
tf2_ros::Buffer tf_buffer; // order matters, buffer has to be built before listener
tf2_ros::TransformListener tf_listener;

}

You can even do it in the member variable declaration:

class MyNode : public rclcpp::Node
{
public:
    MyNode(rclcpp::NodeOptions options)
        : Node("the_name", options)
    {
    // constructor
    }

private:

// TF 2 stuff
tf2_ros::Buffer tf_buffer{get_clock()};
tf2_ros::TransformListener tf_listener{tf_buffer};

}
edit flag offensive delete link more
1

answered 2023-06-01 14:40:25 -0500

130s gravatar image

Not sure, but I think it's intentional, as in this tutorial (docs.ros.org/en/humble) tf2_ros::Buffer is meant to be only accessed by tf2_ros::TransformListener (and assume it doesn't increase reference counter in its internal logic) while tf_listener_ can potentially be accessed many times.

edit flag offensive delete link more

Comments

Yes, that could be the reasoning, yet it isn't a strict necessity. To me (coming from c++98 and being new to ROS) it was rather confusing.

jrtg gravatar image jrtg  ( 2023-06-02 06:25:55 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2022-11-23 03:59:29 -0500

Seen: 241 times

Last updated: Jun 02 '23