Skip to content

Commit

Permalink
Fix memory leak of MpMcQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Jun 27, 2024
1 parent 1f25e65 commit d1eac57
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions src/mpmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ impl<T, const N: usize> Default for MpMcQueue<T, N> {
}
}

impl<T, const N: usize> Drop for MpMcQueue<T, N> {
fn drop(&mut self) {
// drop all contents currently in the queue
while let Some(_) = self.dequeue() {}
}
}

unsafe impl<T, const N: usize> Sync for MpMcQueue<T, N> where T: Send {}

struct Cell<T> {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d1eac57

Please sign in to comment.