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

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.