[wpilibc] Check for invalid handle in destructors (#7212)

Moved-from objects have invalid handles.
This commit is contained in:
Ryan Blue
2024-10-15 22:56:13 -04:00
committed by GitHub
parent 0bada2e102
commit 2b1c5aa4fc
5 changed files with 29 additions and 18 deletions

View File

@@ -84,10 +84,12 @@ Counter::Counter(EncodingType encodingType,
}
Counter::~Counter() {
try {
SetUpdateWhenEmpty(true);
} catch (const RuntimeError& e) {
e.Report();
if (m_counter != HAL_kInvalidHandle) {
try {
SetUpdateWhenEmpty(true);
} catch (const RuntimeError& e) {
e.Report();
}
}
}

View File

@@ -37,11 +37,13 @@ DigitalOutput::DigitalOutput(int channel) {
}
DigitalOutput::~DigitalOutput() {
// Disable the PWM in case it was running.
try {
DisablePWM();
} catch (const RuntimeError& e) {
e.Report();
if (m_handle != HAL_kInvalidHandle) {
// Disable the PWM in case it was running.
try {
DisablePWM();
} catch (const RuntimeError& e) {
e.Report();
}
}
}

View File

@@ -45,9 +45,11 @@ PWM::PWM(int channel, bool registerSendable) {
}
PWM::~PWM() {
int32_t status = 0;
HAL_SetPWMDisabled(m_handle, &status);
FRC_ReportError(status, "Channel {}", m_channel);
if (m_handle != HAL_kInvalidHandle) {
int32_t status = 0;
HAL_SetPWMDisabled(m_handle, &status);
FRC_ReportError(status, "Channel {}", m_channel);
}
}
void PWM::SetPulseTime(units::microsecond_t time) {

View File

@@ -61,8 +61,12 @@ Relay::Relay(int channel, Relay::Direction direction)
Relay::~Relay() {
int32_t status = 0;
HAL_SetRelay(m_forwardHandle, false, &status);
HAL_SetRelay(m_reverseHandle, false, &status);
if (m_forwardHandle != HAL_kInvalidHandle) {
HAL_SetRelay(m_forwardHandle, false, &status);
}
if (m_reverseHandle != HAL_kInvalidHandle) {
HAL_SetRelay(m_reverseHandle, false, &status);
}
}
void Relay::Set(Relay::Value value) {

View File

@@ -90,10 +90,11 @@ TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) {
}
TimedRobot::~TimedRobot() {
int32_t status = 0;
HAL_StopNotifier(m_notifier, &status);
FRC_ReportError(status, "StopNotifier");
if (m_notifier != HAL_kInvalidHandle) {
int32_t status = 0;
HAL_StopNotifier(m_notifier, &status);
FRC_ReportError(status, "StopNotifier");
}
}
void TimedRobot::AddPeriodic(std::function<void()> callback,