Fix memory leak of MpMcQueue

This commit is contained in:
Sosthène Guédon 2024-06-27 14:33:57 +02:00
parent 1f25e658ca
commit 7cfdb8482e
No known key found for this signature in database
GPG Key ID: 36DA48A4C827B354
2 changed files with 20 additions and 0 deletions

View File

@ -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

View File

@ -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 self.dequeue().is_some() {}
}
}
unsafe impl<T, const N: usize> Sync for MpMcQueue<T, N> where T: Send {}
struct Cell<T> {
@ -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();