From bc984234e92a13f8577713237186b5adf7040eb9 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 10 Apr 2026 12:30:00 -0700 Subject: [PATCH] [cmd2] CommandScheduler::Cancel(): Don't mutate in for loop (#8743) Erasing elements from a container while iterating over the container may not be well-defined. --- .../src/main/native/cpp/frc2/command/CommandScheduler.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/commandsv2/src/main/native/cpp/frc2/command/CommandScheduler.cpp b/commandsv2/src/main/native/cpp/frc2/command/CommandScheduler.cpp index 1685d62a2a..a43adf2fb2 100644 --- a/commandsv2/src/main/native/cpp/frc2/command/CommandScheduler.cpp +++ b/commandsv2/src/main/native/cpp/frc2/command/CommandScheduler.cpp @@ -315,11 +315,15 @@ void CommandScheduler::Cancel(Command* command, for (auto&& action : m_impl->interruptActions) { action(*command, interruptor); } + wpi::util::SmallVector toErase; for (auto&& requirement : m_impl->requirements) { if (requirement.second == command) { - m_impl->requirements.erase(requirement.first); + toErase.push_back(requirement.first); } } + for (auto* subsystem : toErase) { + m_impl->requirements.erase(subsystem); + } m_watchdog.AddEpoch(command->GetName() + ".End(true)"); }