From 091f29fcd38461046520e3f5cabd396777df88f6 Mon Sep 17 00:00:00 2001 From: Adam Aposhian Date: Tue, 13 Feb 2024 15:18:15 -0700 Subject: [PATCH] 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 Co-authored-by: Chris Lalancette --- rclcpp_components/src/node_main.cpp.in | 56 ++++++++++++++------------ 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/rclcpp_components/src/node_main.cpp.in b/rclcpp_components/src/node_main.cpp.in index d2a0e84601..7d621aac9f 100644 --- a/rclcpp_components/src/node_main.cpp.in +++ b/rclcpp_components/src/node_main.cpp.in @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include #include @@ -33,41 +34,46 @@ int main(int argc, char * argv[]) @executor@ exec; rclcpp::NodeOptions options; options.arguments(args); - std::vector node_wrappers; std::string library_name = "@library_name@"; std::string class_name = "rclcpp_components::NodeFactoryTemplate<@component@>"; RCLCPP_DEBUG(logger, "Load library %s", library_name.c_str()); auto loader = std::make_unique(library_name); - auto classes = loader->getAvailableClasses(); - for (const auto & clazz : classes) { - std::string name = clazz.c_str(); - if (name.compare(class_name) == 0) { - RCLCPP_DEBUG(logger, "Instantiate class %s", clazz.c_str()); - std::shared_ptr node_factory = nullptr; - try { - node_factory = loader->createInstance(clazz); - } catch (const std::exception & ex) { - RCLCPP_ERROR(logger, "Failed to load library %s", ex.what()); - return 1; - } catch (...) { - RCLCPP_ERROR(logger, "Failed to load library"); - return 1; - } - auto wrapper = node_factory->create_node_instance(options); - auto node = wrapper.get_node_base_interface(); - node_wrappers.push_back(wrapper); - exec.add_node(node); - } + std::vector classes = loader->getAvailableClasses(); + + if (std::find( + classes.begin(), + classes.end(), + class_name) == classes.end()) { + RCLCPP_ERROR( + logger, + "Class %s not found in library %s", + class_name.c_str(), + library_name.c_str()); + return 1; + } + RCLCPP_DEBUG(logger, "Instantiate class %s", class_name.c_str()); + std::shared_ptr node_factory = nullptr; + try { + node_factory = loader->createInstance(class_name); + } catch (const std::exception & ex) { + RCLCPP_ERROR(logger, "Failed to load library %s", ex.what()); + return 1; + } catch (...) { + RCLCPP_ERROR(logger, "Failed to load library"); + return 1; } + // Scope to destruct node_wrapper before shutdown + { + rclcpp_components::NodeInstanceWrapper node_wrapper = node_factory->create_node_instance(options); + rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node = node_wrapper.get_node_base_interface(); + exec.add_node(node); - exec.spin(); + exec.spin(); - for (auto wrapper : node_wrappers) { - exec.remove_node(wrapper.get_node_base_interface()); + exec.remove_node(node_wrapper.get_node_base_interface()); } - node_wrappers.clear(); rclcpp::shutdown();