Skip to content

Commit 091f29f

Browse files
crash on no class found (#2415)
* crash on no class found * error on no class found instead of no callback groups Signed-off-by: Adam Aposhian <[email protected]> Co-authored-by: Chris Lalancette <[email protected]>
1 parent 38bcda4 commit 091f29f

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

rclcpp_components/src/node_main.cpp.in

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include <algorithm>
1516
#include <memory>
1617
#include <string>
1718
#include <vector>
@@ -33,41 +34,46 @@ int main(int argc, char * argv[])
3334
@executor@ exec;
3435
rclcpp::NodeOptions options;
3536
options.arguments(args);
36-
std::vector<rclcpp_components::NodeInstanceWrapper> node_wrappers;
3737

3838
std::string library_name = "@library_name@";
3939
std::string class_name = "rclcpp_components::NodeFactoryTemplate<@component@>";
4040

4141
RCLCPP_DEBUG(logger, "Load library %s", library_name.c_str());
4242
auto loader = std::make_unique<class_loader::ClassLoader>(library_name);
43-
auto classes = loader->getAvailableClasses<rclcpp_components::NodeFactory>();
44-
for (const auto & clazz : classes) {
45-
std::string name = clazz.c_str();
46-
if (name.compare(class_name) == 0) {
47-
RCLCPP_DEBUG(logger, "Instantiate class %s", clazz.c_str());
48-
std::shared_ptr<rclcpp_components::NodeFactory> node_factory = nullptr;
49-
try {
50-
node_factory = loader->createInstance<rclcpp_components::NodeFactory>(clazz);
51-
} catch (const std::exception & ex) {
52-
RCLCPP_ERROR(logger, "Failed to load library %s", ex.what());
53-
return 1;
54-
} catch (...) {
55-
RCLCPP_ERROR(logger, "Failed to load library");
56-
return 1;
57-
}
58-
auto wrapper = node_factory->create_node_instance(options);
59-
auto node = wrapper.get_node_base_interface();
60-
node_wrappers.push_back(wrapper);
61-
exec.add_node(node);
62-
}
43+
std::vector<std::string> classes = loader->getAvailableClasses<rclcpp_components::NodeFactory>();
44+
45+
if (std::find(
46+
classes.begin(),
47+
classes.end(),
48+
class_name) == classes.end()) {
49+
RCLCPP_ERROR(
50+
logger,
51+
"Class %s not found in library %s",
52+
class_name.c_str(),
53+
library_name.c_str());
54+
return 1;
55+
}
56+
RCLCPP_DEBUG(logger, "Instantiate class %s", class_name.c_str());
57+
std::shared_ptr<rclcpp_components::NodeFactory> node_factory = nullptr;
58+
try {
59+
node_factory = loader->createInstance<rclcpp_components::NodeFactory>(class_name);
60+
} catch (const std::exception & ex) {
61+
RCLCPP_ERROR(logger, "Failed to load library %s", ex.what());
62+
return 1;
63+
} catch (...) {
64+
RCLCPP_ERROR(logger, "Failed to load library");
65+
return 1;
6366
}
67+
// Scope to destruct node_wrapper before shutdown
68+
{
69+
rclcpp_components::NodeInstanceWrapper node_wrapper = node_factory->create_node_instance(options);
70+
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node = node_wrapper.get_node_base_interface();
71+
exec.add_node(node);
6472

65-
exec.spin();
73+
exec.spin();
6674

67-
for (auto wrapper : node_wrappers) {
68-
exec.remove_node(wrapper.get_node_base_interface());
75+
exec.remove_node(node_wrapper.get_node_base_interface());
6976
}
70-
node_wrappers.clear();
7177

7278
rclcpp::shutdown();
7379

0 commit comments

Comments
 (0)