4

I present a simple UIViewController using this simple code

@IBAction func addNewFeed(sender: UIBarButtonItem)
{

    var alertView: UIAlertController? = UIAlertController(title: NSLocalizedString("New Feed", comment: "Titolo popup creazione feed"),
        message: NSLocalizedString("Insert the Title and the Link for the new Feed.", comment: "Messaggio creazione nuovo feed"),
        preferredStyle: UIAlertControllerStyle.Alert)


    alertView!.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "Annulla popup creazione nuovo feed"),
        style: UIAlertActionStyle.Cancel,
        handler: nil))

    presentViewController(alertView!, animated: true, completion: nil)

}

When i push a button on my interface i call this IBAction and UIAlertController appears. But when i click on Cancel button to dismiss the controller Leak Tool found a leak as you can see in this image:

enter image description here

I have tried putting a closure like this in handler parameter:

alertView!.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "Annulla popup creazione nuovo feed"),
        style: UIAlertActionStyle.Cancel,
        handler: {[weak self] action in self!.dismissViewControllerAnimated(true, completion: nil)
        alertView = nil
        }))

but there's always that leak.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
msalafia
  • 2,703
  • 5
  • 23
  • 34
  • Looks like it is an iOS bug. Check out [this question.](http://stackoverflow.com/questions/26247221/ios-8-only-memory-leak-with-uialertcontroller-or-uiactionsheet/30312928#30312928) – aksh1t Jun 05 '15 at 12:29

1 Answers1

1

UIViewControllerhas lots of traps to fall into.

Ash Furrow addresses many of the memory problems in this blog post. He tried the weak self thing, but settled on using a local variable that is then used in the closure.

Gene De Lisa
  • 3,628
  • 1
  • 21
  • 36