title: Strong reference cycle for closures in Swift
body: <p>I was going through the documentation (chapter on “Automatic Reference Counting” section “Strong Reference Cycles for Closures”) and I can't seem to figure out cases, when defining a class, in which I should keep a strong reference to <code>self</code> (the instance of that class) in a closure to a property.
  Capture Lists seem always the best solution to avoid memory leaks, and I really can't think of any scenarios in which I should keep a strong reference cycle.</p>
  
  <p>Here are the examples that the documentation gives:</p>
  
  <pre><code>class HTMLElement {
  
      let name: String
      let text: String?
  
      // Without Capture List 
      @lazy var asHTML: () -&gt; String = {
          if let text = self.text {
              return "&lt;\(self.name)&gt;\(text)&lt;/\(self.name)&gt;"
          } else {
              return "&lt;\(self.name) /&gt;"
          }
      }
  
      init(name: String, text: String? = nil) {
          self.name = name
          self.text = text
      }
  
      deinit {
          println("\(name) is being deinitialized")
      }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>class HTMLElement {
  
      let name: String
      let text: String?
  
      // With Capture List
      @lazy var asHTML: () -&gt; String = {
          [unowned self] in
          if let text = self.text {
              return "&lt;\(self.name)&gt;\(text)&lt;/\(self.name)&gt;"
          } else {
              return "&lt;\(self.name) /&gt;"
          }
      }
  
      init(name: String, text: String? = nil) {
          self.name = name
          self.text = text
      }
  
      deinit {
          println("\(name) is being deinitialized")
      }
  
  }
  </code></pre>