UIAlertController内存泄漏/问题-Swift [英] UIAlertController memory leak/issues - Swift

查看:191
本文介绍了UIAlertController内存泄漏/问题-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Instruments显示仅打开和关闭警报控制器会导致内存泄漏.

Instruments shows a memory leak from simply opening and closing the alert controller.

@IBAction func delBtnAc(sender: AnyObject) {

    let deleteAlert = UIAlertController(title: "Delete Image?", message: "", preferredStyle: .Alert)

    let cancelIt = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

    deleteAlert.addAction(cancelIt)
    presentViewController(deleteAlert, animated: true, completion: nil)
}

我已将警报减少为仅用于测试的取消按钮.

I have reduced the alert to only a cancel button for testing.

删除中的deleteAlert.dismissViewController.固定的保留周期,但仍显示内存泄漏.也许是错误.

Edited: Removed deleteAlert.dismissViewController in closure. Fixed retain cycle, but still shows a memory leak. Perhaps a bug.

推荐答案

您的警报操作的完成处理程序对警报控制器有很强的引用.

Your alert action's completion handler has a strong reference to your alert controller.

您的警报操作对其完成处理程序有强烈的参考.

Your alert action has a strong reference to its completion handler.

您的警报控制器对警报操作有很强的参考力.

Your alert controller has a strong reference to the alert action.

所以在这里,我们有一个经典的保留周期.

So here we have a classic retain cycle.

问题是从完成处理程序到警报控制器本身的强大引用,在这种情况下,恰好完全没有必要.运行适当的完成处理程序后,警报控制器将自行关闭.

The problem is the strong reference from the completion handler to the alert controller itself, which in this case, happens to be completely unnecessary. The alert controller dismisses itself after running the appropriate completion handler.

我们可以完全消除线条.

We can completely eliminate the line.

如果我们在完成处理程序中执行非冗余操作,则需要创建对完成处理程序的弱引用,以便可以在完成处理程序中使用它.

If we were doing something non-redundant in the completion handler, we would need to create a weak reference to the completion handler so that we could use that in the completion handler.

这篇关于UIAlertController内存泄漏/问题-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆