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

Revision history [back]

click to hide/show revision 1
initial version

Looks like there might be a bug in our code that allows for various types of callbacks in our code.

If you do something like this:

std::function<void(const std_msgs::msg::String::SharedPtr msg)> bound_callback_func =
  std::bind(callback_func, std::placeholders::_1, topic_name);

And then pass that to create_subscription() then it seems to work correctly.

But I can also reproduce your issue, do you think you could provide a simple, compilable example in an issue on rclcpp?

https://github.com/ros2/rclcpp/issues/new

Please make sure that you follow the new issue guidelines.

Another alternative would be to use lambda to capture the topic name, for example:

auto callback_factory = [](const std::string & a_topic_name) {
  return [a_topic_name](const std_msgs::msg::String::SharedPtr msg) -> void {
    printf("topic_name: %s, data: %s\n", a_topic_name.c_str(), msg->data.c_str());
  };
};

And then use it when making each subscription:

sub_ = create_subscription<std_msgs::msg::String>(topic_name, callback_factory("topic1"));
sub_ = create_subscription<std_msgs::msg::String>(topic_name, callback_factory("topic2"));