I have a parent class and a child class. The parent has a strong reference to the child, and the child has an unowned reference to the parent. During deinit of the parent, I want the child to do some cleaning up, and that involves calling the parent:
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
let parent = Parent()
}
}
class Parent : NSObject
{
override init()
{
super.init()
child.doStuff()
}
deinit
{
child.doStuff()
}
lazy private var child : Child = Child(parent : self)
}
class Child : NSObject
{
init(parent : NSObject)
{
self.parent = parent
}
func doStuff()
{
println(self.parent)
}
deinit
{
}
private unowned var parent : NSObject
}
Unfortunately, calling doStuff()
during deinit of the parent causes a crash, as it uses self.parent
:
libswiftCore.dylib`_swift_abortRetainUnowned:
0x111e91740 <+0>: leaq 0x1e6cd(%rip), %rax ; "attempted to retain deallocated object"
0x111e91747 <+7>: movq %rax, 0x58612(%rip) ; gCRAnnotations + 8
0x111e9174e <+14>: int3
-> 0x111e9174f <+15>: nop
As far as I understand, the parent should still exist because deinit of the parent has not yet completed. Yet this error seems to suggest that the child can no longer access its unowned
reference to the parent.
Can anyone shed any light on this?