diff --git a/rclcpp/src/rclcpp/signal_handler.cpp b/rclcpp/src/rclcpp/signal_handler.cpp index debad74209..ab981be09f 100644 --- a/rclcpp/src/rclcpp/signal_handler.cpp +++ b/rclcpp/src/rclcpp/signal_handler.cpp @@ -46,20 +46,40 @@ dispatch_semaphore_t SignalHandler::signal_handler_sem_; sem_t SignalHandler::signal_handler_sem_; #endif +// The logger must be initialized before the local static variable signal_handler, +// from the method get_global_signal_handler(), so that it is destructed after +// it, because the destructor of SignalHandler uses this logger object. +static rclcpp::Logger g_logger = rclcpp::get_logger("rclcpp"); + +rclcpp::Logger & +SignalHandler::get_logger() +{ + return g_logger; +} + SignalHandler & SignalHandler::get_global_signal_handler() { + // This is initialized after the g_logger static global, ensuring + // SignalHandler::get_logger() may be called from the destructor of + // SignalHandler, according to this: + // + // Variables declared at block scope with the specifier static have static + // storage duration but are initialized the first time control passes + // through their declaration (unless their initialization is zero- or + // constant-initialization, which can be performed before the block is + // first entered). On all further calls, the declaration is skipped. + // + // -- https://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables + // + // Which is guaranteed to occur after static initialization for global (see: + // https://en.cppreference.com/w/cpp/language/initialization#Static_initialization), + // which is when g_logger will be initialized. + // And destruction will occur in the reverse order. static SignalHandler signal_handler; return signal_handler; } -rclcpp::Logger & -SignalHandler::get_logger() -{ - static rclcpp::Logger logger = rclcpp::get_logger("rclcpp"); - return logger; -} - bool SignalHandler::install() {