[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.
This commit is contained in:
Peter Johnson
2026-04-10 12:30:00 -07:00
committed by GitHub
parent ea06f0b257
commit bc984234e9

View File

@@ -315,11 +315,15 @@ void CommandScheduler::Cancel(Command* command,
for (auto&& action : m_impl->interruptActions) {
action(*command, interruptor);
}
wpi::util::SmallVector<Subsystem*, 8> 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)");
}