title: Can a child object refer to its parent during deinit of the parent?
body: <p>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:</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>Unfortunately, calling <code>doStuff()</code> during deinit of the parent causes a crash, as it uses <code>self.parent</code>:</p>
  
  <pre><code>libswiftCore.dylib`_swift_abortRetainUnowned:
      0x111e91740 &lt;+0&gt;:  leaq   0x1e6cd(%rip), %rax       ; "attempted to retain deallocated object"
      0x111e91747 &lt;+7&gt;:  movq   %rax, 0x58612(%rip)       ; gCRAnnotations + 8
      0x111e9174e &lt;+14&gt;: int3   
  -&gt;  0x111e9174f &lt;+15&gt;: nop    
  </code></pre>
  
  <p>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 <code>unowned</code> reference to the parent. </p>
  
  <p>Can anyone shed any light on this? </p>