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

If a plugin is initialized inside a try/catch, it doesn't keep the subscribers running

asked 2019-02-02 04:32:40 -0500

Edu_ gravatar image

updated 2019-02-02 16:13:21 -0500

Hi all, Let me give first an introduction of my problem. I'm playing around with the "Creating your own plugin" tutorial. Until now I have 2 plugins, each of them has a subscriber callback to different topics. All the plugins are created correctly. The problem starts when I want to load them. The following is the code I'm using for that:

int main(int argc, char** argv)
{
  ros::init(argc, argv, "polygon_loader_node");
  ros::NodeHandle private_nh("~/plugin_test");
  ros::NodeHandle g_nh;
  pluginlib::ClassLoader<polygon_base::RegularPolygon> poly_loader("plugin_test", "polygon_base::RegularPolygon");
  boost::shared_ptr<polygon_base::RegularPolygon> polygon_plugins = poly_loader.createInstance("polygon_plugins::polygonPlugins");
  polygon_plugins->initialize();
  try
  {
    boost::shared_ptr<polygon_base::RegularPolygon> plugin = poly_loader.createInstance(polygon_plugins::Triangle);
    plugin->initialize();
  }
  catch(pluginlib::PluginlibException& ex)
  {
    ROS_ERROR("The plugin failed to load for some reason. Error: %s", ex.what());
  }
 ros::spin();
 return 0;
}

In the code above, both plugins are initialized, however I can only go into the callback of the polygonPlugins one. However if I remove the try/catch, so both plugins are in the same level, then I can go into the Triangle plugin callback as well. I suspect is something related with the location of the ros::spin(), however I don't know exactly the issue. I appreciate in advance your help. Cheers.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-02-02 17:28:35 -0500

ahendrix gravatar image

This is pretty typical C++ object lifetime: a C++ variable only lasts for the duration of block where it is created, and it is destroyed at the end of the block.

A block is the scope that starts at { and ends at the matching }

Therefore, since your boost::shared_ptr<polygon_base::RegularPolygon> plugin variable is declared inside the try {}, it is also destructed at the end of that block.

You can fix this by moving the variable declaration outside of the try {} block, like this:

boost::shared_ptr<polygon_base::RegularPolygon> plugin;
try {
    plugin = poly_loader.createInstance(polygon_plugins::Triangle);
    plugin->initialize();
} catch(pluginlib::PluginlibException& ex) {
    ROS_ERROR("The plugin failed to load for some reason. Error: %s", ex.what());
}

Now boost::shared_ptr<polygon_base::RegularPolygon> plugin is declared within main's block (or scope), and lasts until the end of main.

edit flag offensive delete link more

Comments

Cool, it worked! Thank you very much!

Edu_ gravatar image Edu_  ( 2019-02-02 17:31:08 -0500 )edit

Please mark the question as answered by clicking the checkmark to the left of the answer, if you feel it has been answered.

gvdhoorn gravatar image gvdhoorn  ( 2019-02-03 09:27:16 -0500 )edit

Question Tools

Stats

Asked: 2019-02-02 02:52:23 -0500

Seen: 166 times

Last updated: Feb 02 '19