From 1921d7b79a5224117ef1d2a07dbe199d85c5e623 Mon Sep 17 00:00:00 2001 From: Dustin Spicuzza Date: Wed, 12 Feb 2025 01:05:22 -0500 Subject: [PATCH] [wpilibc] Remove Alert magic static with map lookup (#7712) The magic static adds yet another thing that needs to be reset if you want to run a unit test with a completely reset wpilib state. Use the existing Sendable static map to store the data instead. This leaks memory if ResetSmartDashboardInstance is called. --- wpilibc/src/main/native/cpp/Alert.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/wpilibc/src/main/native/cpp/Alert.cpp b/wpilibc/src/main/native/cpp/Alert.cpp index f74f0ab759..7cce523377 100644 --- a/wpilibc/src/main/native/cpp/Alert.cpp +++ b/wpilibc/src/main/native/cpp/Alert.cpp @@ -82,17 +82,18 @@ class Alert::SendableAlerts : public nt::NTSendable, * @return the SendableAlerts for the group */ static SendableAlerts& ForGroup(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, inserted] = groups.try_emplace(group); - SendableAlerts& sendable = iter->second; - if (inserted) { - frc::SmartDashboard::PutData(group, &iter->second); + SendableAlerts* salert = nullptr; + try { + auto* sendable = frc::SmartDashboard::GetData(group); + salert = dynamic_cast(sendable); + } catch (frc::RuntimeError&) { } - return sendable; + if (!salert) { + // this leaks if ResetSmartDashboardInstance is called, but that's fine + salert = new Alert::SendableAlerts; + frc::SmartDashboard::PutData(group, salert); + } + return *salert; } private: