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

Revision history [back]

As pointed out in the comment, in your C++ code the object is only destroyed at the end of the program when the scope for the shared pointed ends. At that time ros has already shut down and you can no longer publish. However, you can force the destruction of the shared pointer before shutdown, but limiting its scope a bit more. I haven't tested this, but the following should do the trick:

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  {
    auto node = std::make_shared<Test>();
    rclcpp::spin(node);
  }
  rclcpp::shutdown();
  return 0;
}

As pointed out in the comment, in your C++ code the object is only destroyed at the end of the program when the scope for the shared pointed ends. At that time ros has already shut down and you can no longer publish. However, you can force the destruction of the shared pointer before shutdown, but limiting its scope a bit more. I haven't tested this, but the following should do the trick:

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  {
    auto node = std::make_shared<Test>();
    rclcpp::spin(node);
  }
  rclcpp::spin_some(); // need to let ros spin a bit more to process the publish queued by the destructor
  rclcpp::shutdown();
  return 0;
}