Memory leak with UIAlertController in Swift
I am presenting 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 click a button on my interface, I call this IBAction and the UIAlertController appears. But when I click the Cancel button to close the controller, the Leak Tool has detected a leak, as you can see in this image:
I tried to put a closure like this in the 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 is always a leak.
+3
Andorath
source
to share
1 answer
UIViewController
has many traps to fall.
Ash Furrow addresses many of the memory issues in this post. He tried a weak self, but decided to use a local variable, which is then used in the closure.
+1
Gene de lisa
source
to share