From 749c7adb1315e5d84161353e99e2dde20a8de2a6 Mon Sep 17 00:00:00 2001 From: Prateek Machiraju Date: Thu, 31 Dec 2020 23:35:17 -0500 Subject: [PATCH] [command] Fix use-after-free in CommandScheduler (#3024) --- .../src/main/native/cpp/frc2/command/CommandScheduler.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp index f13c53fbe9..3acd9b5fdd 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp @@ -84,6 +84,8 @@ CommandScheduler::~CommandScheduler() { auto scheduler = frc::LiveWindow::GetInstance(); scheduler->enabled = nullptr; scheduler->disabled = nullptr; + + std::unique_ptr().swap(m_impl); } CommandScheduler& CommandScheduler::GetInstance() { @@ -312,6 +314,10 @@ Command* CommandScheduler::GetDefaultCommand(const Subsystem* subsystem) const { } void CommandScheduler::Cancel(Command* command) { + if (!m_impl) { + return; + } + if (m_impl->inRunLoop) { m_impl->toCancel.emplace_back(command); return;