Skip to content

Commit 662941d

Browse files
authored
Merge pull request #483 from Nitrokey/mpmc-drop
Fix memory leak of `MpMcQueue`
2 parents d1c47c3 + 7cfdb84 commit 662941d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3131
- Fixed the list of implemented data structures in the crate docs, by adding `Deque`,
3232
`HistoryBuffer` and `SortedLinkedList` to the list.
3333
- Fixed `MpMcQueue` with `mpmc_large` feature.
34+
- Fix missing `Drop` for `MpMcQueue`
3435

3536
## [v0.8.0] - 2023-11-07
3637

src/mpmc.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ impl<T, const N: usize> Default for MpMcQueue<T, N> {
194194
}
195195
}
196196

197+
impl<T, const N: usize> Drop for MpMcQueue<T, N> {
198+
fn drop(&mut self) {
199+
// drop all contents currently in the queue
200+
while self.dequeue().is_some() {}
201+
}
202+
}
203+
197204
unsafe impl<T, const N: usize> Sync for MpMcQueue<T, N> where T: Send {}
198205

199206
struct Cell<T> {
@@ -306,6 +313,18 @@ mod tests {
306313
// Ensure a `MpMcQueue` containing `!Send` values stays `!Send` itself.
307314
assert_not_impl_any!(MpMcQueue<*const (), 4>: Send);
308315

316+
#[test]
317+
fn memory_leak() {
318+
droppable!();
319+
320+
let q = Q2::new();
321+
q.enqueue(Droppable::new()).unwrap_or_else(|_| panic!());
322+
q.enqueue(Droppable::new()).unwrap_or_else(|_| panic!());
323+
drop(q);
324+
325+
assert_eq!(Droppable::count(), 0);
326+
}
327+
309328
#[test]
310329
fn sanity() {
311330
let q = Q2::new();

0 commit comments

Comments
 (0)