diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bea10baa6..f933d97612 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fixed the list of implemented data structures in the crate docs, by adding `Deque`, `HistoryBuffer` and `SortedLinkedList` to the list. - Fixed `MpMcQueue` with `mpmc_large` feature. +- Fix missing `Drop` for `MpMcQueue` ## [v0.8.0] - 2023-11-07 diff --git a/src/mpmc.rs b/src/mpmc.rs index 3da72d7f31..bb9b1fdea4 100644 --- a/src/mpmc.rs +++ b/src/mpmc.rs @@ -194,6 +194,13 @@ impl Default for MpMcQueue { } } +impl Drop for MpMcQueue { + fn drop(&mut self) { + // drop all contents currently in the queue + while self.dequeue().is_some() {} + } +} + unsafe impl Sync for MpMcQueue where T: Send {} struct Cell { @@ -306,6 +313,18 @@ mod tests { // Ensure a `MpMcQueue` containing `!Send` values stays `!Send` itself. assert_not_impl_any!(MpMcQueue<*const (), 4>: Send); + #[test] + fn memory_leak() { + droppable!(); + + let q = Q2::new(); + q.enqueue(Droppable::new()).unwrap_or_else(|_| panic!()); + q.enqueue(Droppable::new()).unwrap_or_else(|_| panic!()); + drop(q); + + assert_eq!(Droppable::count(), 0); + } + #[test] fn sanity() { let q = Q2::new();