Skip to content

Commit

Permalink
fix: try fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Dec 25, 2024
1 parent 65e7ecf commit bb15d37
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions crossbeam-channel/src/flavors/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,42 @@ impl<T> Channel<T> {
}
}

impl<T> Drop for Channel<T> {
fn drop(&mut self) {
if !self.is_disconnected() {
let head = *self.head.get_mut();
let tail = *self.tail.get_mut();
let hix = head & (self.mark_bit - 1);
let tix = tail & (self.mark_bit - 1);

let len = if hix < tix {
tix - hix
} else if hix > tix {
self.cap() - hix + tix
} else if (tail & !self.mark_bit) == head {
0
} else {
self.cap()
};

for i in 0..len {
// Compute the index of the next slot holding a message.
let index = if hix + i < self.cap() {
hix + i
} else {
hix + i - self.cap()
};

unsafe {
debug_assert!(index < self.buffer.len());
let slot = self.buffer.get_unchecked_mut(index);
(*slot.msg.get()).assume_init_drop();
}
}
}
}
}

/// Receiver handle to a channel.
pub(crate) struct Receiver<'a, T>(&'a Channel<T>);

Expand Down

0 comments on commit bb15d37

Please sign in to comment.