Skip to content

Commit

Permalink
Fix disconnect race crash. (#147)
Browse files Browse the repository at this point in the history
It was possible for the Target object to be destructed before it
stopped.
This manifested as a crash when freed memory was accessed.
Add a flag to wait and check.
  • Loading branch information
gkoh authored Dec 27, 2024
1 parent f1e892c commit bb4b632
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/FurbleControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class Control {
protected:
Target(Camera *camera);

volatile bool m_Stopped = false;

private:
static constexpr UBaseType_t m_QueueLength = 8;

Expand Down
9 changes: 9 additions & 0 deletions src/FurbleControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void Control::Target::task(void) {
}
}
task_exit:
m_Stopped = true;
vTaskDelete(NULL);
}

Expand Down Expand Up @@ -212,10 +213,18 @@ void Control::disconnect(void) {

const std::lock_guard<std::mutex> lock(m_Mutex);

// send disconnect
for (const auto &target : m_Targets) {
target->sendCommand(CMD_DISCONNECT);
}

// wait for tasks to finish
for (const auto &target : m_Targets) {
do {
vTaskDelay(pdMS_TO_TICKS(1));
} while (!target.get()->m_Stopped);
}

m_Targets.clear();
m_State = STATE_IDLE;
}
Expand Down

0 comments on commit bb4b632

Please sign in to comment.