Skip to content

TransformBroadcaster takes shared pointer instead of raw node #554

Closed

Description

@ijnek

Bug report

Required Info:

  • Operating System: Ubuntu 22.04 Jammy
  • Installation type: Source
  • Version or commit hash: rolling
  • DDS implementation: Rolling Default

Description

The TransformBroadcaster constructor should take a NodeT, but tests in tf2_ros/test/test_transform_broadcaster.cpp pass a pointer to a rclcpp::Node to the broadcaster (and the tests pass although they shouldn't).

Changes in #552 exposed this issue, as it depends on NodeT being a pointer to a rclcpp::Node.

In Writing a broadcaster (C++), a node instance is passed to the constructor as below:

tf_broadcaster_ = std::make_unique<tf2_ros::TransformBroadcaster>(*this);

But in the tf2_ros/test/test_transform_broadcaster.cpp, a shared pointer is provided.

TEST(tf2_test_transform_broadcaster, transform_broadcaster_rclcpp_node)
{
  auto node = rclcpp::Node::make_shared("tf2_ros_message_filter");

  tf2_ros::TransformBroadcaster tfb(node);
}

This was originally reported in ros-perception/image_pipeline#775.

FYI @alsora

Steps to reproduce issue

mkdir -p geometry2_ws/src
cd "$_"
git clone [email protected]:ijnek/geometry2 -b ijnek-bug-test-case
cd ../
colcon build

Expected behavior

Compiles fine.

Actual behavior

Doesn't compile.

--- stderr: tf2_ros                               
In file included from /home/ijnek/tmp_workspaces/geometry2_ws/src/geometry2/tf2_ros/test/test_transform_broadcaster.cpp:31:
/home/ijnek/tmp_workspaces/geometry2_ws/src/geometry2/tf2_ros/include/tf2_ros/transform_broadcaster.h: In instantiation of ‘tf2_ros::TransformBroadcaster::TransformBroadcaster(NodeT&&, const rclcpp::QoS&, const rclcpp::PublisherOptionsWithAllocator<AllocatorT>&) [with NodeT = rclcpp::Node&; AllocatorT = std::allocator<void>]’:
/home/ijnek/tmp_workspaces/geometry2_ws/src/geometry2/tf2_ros/test/test_transform_broadcaster.cpp:57:41:   required from here
/home/ijnek/tmp_workspaces/geometry2_ws/src/geometry2/tf2_ros/include/tf2_ros/transform_broadcaster.h:71:7: error: base operand of ‘->’ has non-pointer type ‘rclcpp::Node’
   71 |       node->get_node_parameters_interface(),
      |       ^~~~
gmake[2]: *** [CMakeFiles/tf2_ros_test_transform_broadcaster.dir/build.make:76: CMakeFiles/tf2_ros_test_transform_broadcaster.dir/test/test_transform_broadcaster.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:510: CMakeFiles/tf2_ros_test_transform_broadcaster.dir/all] Error 2
gmake[1]: *** Waiting for unfinished jobs....
gmake: *** [Makefile:146: all] Error 2
---
Failed   <<< tf2_ros [14.3s, exited with code 2]

Activity

  1. changed the title [-]TransformBroadcaster takes shared pointer or raw node?[/-] [+]TransformBroadcaster takes shared pointer instead of raw node[/+] on Sep 20, 2022
  2. alsora commented on Sep 21, 2022

    @alsora
    Contributor

    Yes, the changes in #552 require that the NodeT passed to the constructor of TransformBroadcaster is some sort of pointer.

    Note that this requirement was already present in the constructor of TransformListener: the node passed to the constructor was then used in the init function as node->get_node_base_interface() (thus assuming that it was a pointer) see https://github.com/ros2/geometry2/pull/552/files#diff-db2161f65f99c88d9aef0e89c8d8b2774fcc52e45c9ccc47a9ab76648842f908L102 and https://github.com/ros2/geometry2/pull/552/files#diff-db2161f65f99c88d9aef0e89c8d8b2774fcc52e45c9ccc47a9ab76648842f908L119

    Before my PR, TransformBroadcaster was accepting either nodes or pointers to nodes and TransformListener was accepting only pointers to nodes.
    Now they both only accept pointers to nodes.
    I wasn't aware of the existence of that tutorial where a node (not a pointer) was used.

    There are different options here:

    1. change the tutorial to use a pointer.
    2. change the tutorial to use a pointer and also add a deprecated constructor that takes a node to preserve backward compatibility.
    3. change both TransformBroadcaster and TransformListener to accept both nodes and pointers to nodes.

    IMO nodes (and ROS entities in general) should be handled via pointers to make it harder to do mistakes, thus my preference would be (2).

  3. ijnek commented on Sep 21, 2022

    @ijnek
    ContributorAuthor

    I agree with option 2 too.

    For the new recommended method, instead of having:

      template<class NodeT, class AllocatorT = std::allocator<void>>
      TransformBroadcaster(
        NodeT && node

    could I suggest having:

    template<class NodeT = rclcpp::Node, class AllocatorT = std::allocator<void>>
      TransformBroadcaster(
        std::shared_ptr<NodeT> node,
    ....

    That's how its done in a function from rclcpp/executors.hpp.

    And yes, we'll have to be careful to consider cases that'll break and add some deprecated methods.

  4. clalancette commented on Sep 21, 2022

    @clalancette
    Contributor

    I agree with option 2 too.

    Yes, I agree with this too. @alsora could you make the updates? Thanks.

  5. alsora commented on Sep 21, 2022

    @alsora
    Contributor

    Yes, will open follow-up PRs soon.
    @ijnek note that your proposed approach would not work with raw pointers.

  6. clalancette commented on Sep 21, 2022

    @clalancette
    Contributor

    Yes, will open follow-up PRs soon.

    Thanks, appreciated.

  7. ijnek commented on Sep 23, 2022

    @ijnek
    ContributorAuthor

    @ijnek note that your proposed approach would not work with raw pointers.

    I was just trying to get across the point that NodeT itself shouldn't be a pointer, but should be the rclcpp node type. That's how it is everywhere else in rclcpp.

    In regards to using shared_ptr vs raw pointer vs reference to pass rclcpp::Node, I don't know what the recommended way is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions