Search code examples
swiftclosuresretain-cycle

Confusion Regarding How to Use Capture Lists to Avoid a Reference Cycle


My custom UIViewController subclass has a stored closure property. The closure signature is defined as to take a single argument of the same type of the class:

class MyViewController {

    var completionHandler : ((MyViewController)->(Void))?

    // ...
}

...the idea being, the object passing itself back as an argument of the handler, a bit like the UIAlertAction initializer.

In addition, and for convenience, I have a factory(-ish) class method:

class func presentInstance(withCompletionHandler handler:((MyViewController)->(Void)))
{
   // ...
}

...that performs the following actions:

  1. Creates an instance of the view controller,
  2. Assigns the completion handler to the property,
  3. Presents it modally from whatever happens to be the top/root view controller at the time of the call.

My view controller is definitely leaking: I set up a breakpoint on deinit() but execution never hits it, even way after I'm done with my view controller and it is dismissed.

I am not sure of how or where I should specify a capture list in order to avoid the cycle. Every example I have come across seems to place it where the closure body is defined, but I can't get my code to compile.

  1. Where I declare the closure property? (how?)

    var completionHandler : ((MyViewController)->(Void))?
    // If so, where does it go?
    
  2. Where I declare the closure parameter?

    class func presentInstance(withCompletionHandler handler:((MyViewController)->(Void)))
    {
    // Again, where could it go?
    
  3. Where I call the above function and pass the closure body?

    MyViewController.presentInstance(withCompletionHandler:{
        [unowned viewController] viewController in 
    
        // ...
    })
    // ^ Use of unresolved identifier viewController
    // ^ Definition conflicts with previous value
    
  4. Where I actually call the closure, towards self? None of these will compile:

    self.completionHandler?(unowned self)
    self.completionHandler?([unowned self] self)
    self.completionHandler?([unowned self], self)
    

Solution

  • Well, it turns out my view controller was being retained by a block, but not the one I was thinking:

    class MyViewController
    {
        deinit(){
            print("Deinit...")
        }
    
        // ...
    
        @IBAction func cancel(sender:UIButton)
        {
            completionHandler(self) 
            // View controller is dismissed, AND
            // deinit() gets called.     
        }
    
        @IBAction func punchIt(sender: UIButton)
        {
            MyWebService.sharedInstance.sendRequest(    
                completion:{ error:NSError? in
    
                     self.completionHandler(self)  
                     // View controller is dismissed, BUT
                     // deinit() does NOT get called. 
                }
            )
        }
    

    ...so it is the closure passed to MyWebService.sharedInstance.sendRequest() that was keeoing my view controller alive. I fixed it by adding a capture list like this:

    MyWebService.sharedInstance.sendRequest(    
                completion:{ [unowned self] error:NSError? in
    

    However, I still don't quite understand why the short-lived completion handler, passed to the web service class, executed once, and disposed, was keeping my view controller alive. That closure, not stored anywhere as a property, should be deallocated as soon as it is exited, right?

    I must be missing something. I guess I'm still not fully thinking in portals closures.