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: