From f3a323f30bf4b2eff864bcb049ab1b98b74eae4e Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Mon, 21 Oct 2024 17:13:28 -0400 Subject: [PATCH] Force SendableRegistry initialization --- wpilibc/src/main/native/cpp/Alert.cpp | 4 ++++ wpiutil/src/main/native/cpp/sendable/SendableRegistry.cpp | 4 ++++ .../src/main/native/include/wpi/sendable/SendableRegistry.h | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/wpilibc/src/main/native/cpp/Alert.cpp b/wpilibc/src/main/native/cpp/Alert.cpp index c9267ad03e0..0ea53ea89e3 100644 --- a/wpilibc/src/main/native/cpp/Alert.cpp +++ b/wpilibc/src/main/native/cpp/Alert.cpp @@ -14,6 +14,7 @@ #include #include +#include #include "frc/Errors.h" #include "frc/smartdashboard/SmartDashboard.h" @@ -21,6 +22,9 @@ using namespace frc; Alert::SendableAlerts& Alert::GetGroupSendable(std::string_view group) { + // Force initialization of SendableRegistry before our magic static to prevent + // incorrect destruction order. + wpi::SendableRegistry::EnsureInitialized(); static wpi::StringMap groups; auto [iter, exists] = groups.try_emplace(group); diff --git a/wpiutil/src/main/native/cpp/sendable/SendableRegistry.cpp b/wpiutil/src/main/native/cpp/sendable/SendableRegistry.cpp index c2b6b8ede77..99ff7f5e1ab 100644 --- a/wpiutil/src/main/native/cpp/sendable/SendableRegistry.cpp +++ b/wpiutil/src/main/native/cpp/sendable/SendableRegistry.cpp @@ -81,6 +81,10 @@ void ResetSendableRegistry() { } // namespace wpi::impl #endif +void SendableRegistry::EnsureInitialized() { + GetInstance(); +} + void SendableRegistry::SetLiveWindowBuilderFactory( std::function()> factory) { GetInstance().liveWindowFactory = std::move(factory); diff --git a/wpiutil/src/main/native/include/wpi/sendable/SendableRegistry.h b/wpiutil/src/main/native/include/wpi/sendable/SendableRegistry.h index 463974930ba..37ed45fe192 100644 --- a/wpiutil/src/main/native/include/wpi/sendable/SendableRegistry.h +++ b/wpiutil/src/main/native/include/wpi/sendable/SendableRegistry.h @@ -26,6 +26,12 @@ class SendableRegistry final { using UID = size_t; + /** + * Initializes the SendableRegistry. This is used to ensure initialization and + * destruction order relative to Sendables. + */ + static void EnsureInitialized(); + /** * Sets the factory for LiveWindow builders. *