我在HomeViewController
中有一些代码。当我打开下一个 Controller 并点击按钮并执行
let strotyboard = UIStoryboard(name: "Anketa", bundle: nil)
if let anketaController = strotyboard.instantiateViewController(withIdentifier: "QuestionnaireViewController") as? QuestionnaireViewController {
anketaController.startNew = true
print("1111 HomeViewController openNext , ", anketaController)
present(anketaController, animated: true, completion: nil)
}
当我返回HomeViewController
并再次点击按钮以打开QuestionnaireViewController
时,这是一条错误消息:"QuestionnaireViewController retain]: message sent to deallocated instance 0x10480a400"
我打印了
print("1111 HomeViewController openNext , ", anketaController)
,第一次点击按钮是:1111 HomeViewController openNext , <.QuestionnaireViewController: 0x10480a400>
在第二个点击的按钮上是:1111 HomeViewController openNext , <.QuestionnaireViewController: 0x10901fa00>
并且该应用在iOS 13上崩溃
最佳答案
奥默尔的答案可能是正确的。我自己创建保留循环有很多麻烦(即使经过很多时间)。帮助我的是this idea of doing an extension to UIViewController that makes a delayed check on the self pointer to make sure it's deallocated。
如果2秒钟后未释放 View Controller ,则您肯定会泄漏,因此断言将停止您的代码,您可以使用XCode的Memory Graph功能,找到 View Controller 并查看使它保持 Activity 状态的原因。
关于ios - 问题:消息发送到已释放实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62691721/