I've made a little linked list, when there's to much nodes in it it crashes during dealloc. (reported: rdar://22189873)
Here is a sample code :
public class ListNode<T> {
var next: ListNode<T>?
weak var previous: ListNode<T>?
let element: T
init(element: T) {
self.element = element
}
}
public struct LinkedList<T> {
public typealias Element = T
private var head: ListNode<T>?
private weak var tail: ListNode<T>?
}
public extension LinkedList {
public mutating func append(element: Element) {
let node = ListNode<T>(element: element)
if head == nil || tail == nil {
self.head = node
self.tail = head
} else {
let old = tail
tail = node
old!.next = tail
tail!.previous = old
}
}
public mutating func removeAll() {
head = nil
}
}
That's pretty simple.
Now here is a simple test :
var count = 500_000
var ll = LinkedList<Int>()
for i in 0..<count {
ll.append(i)
}
ll.removeAll()
It crashes. If count is lower than about 85000 it doesn't crash...
I think it's because it releases nodes recursively once head is set to nil and causes function stack overflow.
What shoul I do ?