Skip to content

Commit

Permalink
Fix a crash when dropping SpiDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Oct 29, 2024
1 parent 7d5e233 commit 30d4874
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ pub struct SpiBusDriver<'d, T>
where
T: BorrowMut<SpiDriver<'d>>,
{
_lock: BusLock,
lock: Option<BusLock>,
handle: spi_device_handle_t,
driver: T,
duplex: Duplex,
Expand All @@ -646,7 +646,7 @@ where
let lock = BusLock::new(handle)?;

Ok(Self {
_lock: lock,
lock: Some(lock),
handle,
driver,
duplex: config.duplex,
Expand Down Expand Up @@ -825,6 +825,10 @@ where
T: BorrowMut<SpiDriver<'d>>,
{
fn drop(&mut self) {
// Need to drop the lock first, because it holds the device
// we are about to remove below
self.lock = None;

esp!(unsafe { spi_bus_remove_device(self.handle) }).unwrap();
}
}
Expand Down

0 comments on commit 30d4874

Please sign in to comment.