问题:发送到释放实例的消息

Problem: message sent to deallocated instance

我在 HomeViewController 中有一些代码。当我打开下一个控制器并点击按钮并执行

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

崩溃

Omer 的回答可能是正确的。我自己创建保留周期时遇到了很多麻烦(即使在相当长的时间之后)。对我有帮助的是 this idea of doing an extension to UIViewController that makes a delayed check on the self pointer to make sure it's deallocated.

如果你的视图控制器在 2 秒后没有被释放,你肯定有泄漏,所以你的断言将停止你的代码,你可以使用 XCode 的内存图功能,找到你的视图控制器,看看是什么让它保持活力。