Skip to content

Commit

Permalink
linked_list: Use a safe loop in Drop
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrik Sverdrup committed Jun 6, 2015
1 parent da0d452 commit a090e1f
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/libcollections/linked_list.rs
Expand Up @@ -626,21 +626,13 @@ impl<T> LinkedList<T> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Drop for LinkedList<T> {
fn drop(&mut self) {
// Dissolve the linked_list in backwards direction
// Dissolve the linked_list in a loop.
// Just dropping the list_head can lead to stack exhaustion
// when length is >> 1_000_000
let mut tail = self.list_tail;
loop {
match tail.resolve() {
None => break,
Some(prev) => {
prev.next.take(); // release Box<Node<T>>
tail = prev.prev;
}
}
while let Some(mut head_) = self.list_head.take() {
self.list_head = head_.next.take();
}
self.length = 0;
self.list_head = None;
self.list_tail = Rawlink::none();
}
}
Expand Down

0 comments on commit a090e1f

Please sign in to comment.