title: Swift arrays/collections memory management
body: <p>I've spent over a week reading various articles online &amp; StackOverflow Q&amp;A on memory management of Swift arrays within <code>UIViewController</code>, I'm still getting crashes in my app. I hope you can help.</p>
  
  <p>I have a parent class controller and multiple subclasses (simplified using examples) that inherit from the parent class, like this.</p>
  
  <pre><code>class ParentViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
      lazy var results: [AnyObject] = [AnyObject]()  //can contain various classes, such as Product, User, etc.
  
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return results.count
      }
  
          ...       
  }
  
  
  class SubViewController: ParentViewController {
  
     ... loads results Array via REST API, then then appends to array using 
     results.append(Product())
  
  }
  </code></pre>
  
  <p>Randomly, I'd get crashes like this:</p>
  
  <pre><code>EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0x0000000a1abb4b17
  Thread : Crashed: com.apple.main-thread
  0  libobjc.A.dylib                0x000000019478fe10 cache_getImp + 16
  1  libobjc.A.dylib                0x0000000194784a6c lookUpImpOrForward + 540
  2  libobjc.A.dylib                0x000000019478fdb8 _objc_msgSend_uncached_impcache + 56
  3  libswiftCore.dylib             0x00000001004d4e40 _ZN5swift12metadataimpl14ValueWitnessesINS0_17ObjCRetainableBoxEE18initializeWithCopyEPNS_11OpaqueValueES5_PKNS_8MetadataE + 24
  4  libswiftCore.dylib             0x000000010036e3cc _TFSa6appendU__fRGSaQ__FQ_T_ + 92
  </code></pre>
  
  <p>Based on my understanding, I believe it's because <code>results</code> is no longer in memory by the time <code>results.append()</code> is called.</p>
  
  <p>What's the best practice to instantiate arrays/collections in UIViewController, especially when there are subclasses? Here are some of my thoughts, any feedback would be appreciated.</p>
  
  <p>1.Use <code>lazy</code> in parent class. This is the base case and causes crashes</p>
  
  <pre><code>lazy var results: [AnyObject] = [AnyObject]()
  </code></pre>
  
  <p>2.Init in <code>SubViewController</code>, this still causes crashes, also, should I deallocate the memory by using <code>deinit()</code>?</p>
  
  <pre><code>in ParentViewController
  var results: [AnyObject]
  
  in SubViewController
      required init(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          self.results = [Product]()
      }
  </code></pre>
  
  <p>3.Make <code>results</code> optional in parent class, this would require all methods in parent class to check for optionals. Should I initialize the array in <code>init</code> in the subclass or in the <code>viewDidLoad()</code>? Also, should I <code>deinit</code> results? to nil in parent class?</p>
  
  <pre><code>in ParentViewController
  var results: [AnyObject]?
  
  deinit {
      results = nil
  }
  
  in SubViewController
      required init(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          self.results = [Product]()
      }
  </code></pre>
  
  <p>Any suggestions would be appreciated!</p>