Skip to content

Commit

Permalink
[wpilibc] Check for invalid handle in destructors
Browse files Browse the repository at this point in the history
Moved-from objects have invalid handles.
  • Loading branch information
rzblue committed Oct 16, 2024
1 parent 0bada2e commit 2b1f9a0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
10 changes: 6 additions & 4 deletions wpilibc/src/main/native/cpp/Counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand Down
12 changes: 7 additions & 5 deletions wpilibc/src/main/native/cpp/DigitalOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand Down
8 changes: 5 additions & 3 deletions wpilibc/src/main/native/cpp/PWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions wpilibc/src/main/native/cpp/Relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions wpilibc/src/main/native/cpp/TimedRobot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2b1f9a0

Please sign in to comment.