ios - 问题:消息发送到已释放实例

标签 ios swift xcode crash

我在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/

相关文章:

ios - UIAccessibility - 画外音不适用于 NSMutableAttributedString

ios - Realm:如何获取数据库的当前大小

ios - 自动布局多个 UITextviews 和标签 - Swift

ios - 如何使用 swift 水平添加 tableview 将 UICollectionView 实现到 UITableviewCell 中?

ios - Xcode 错误 "Could not find Developer Disk Image"

ios - 没有应用内购买的 App Store 托管内容?

ios - 如何在 Xcode 中调整 UIStackView 的 subview ?

swift - 在 Swift 中使用 PresentViewController 传递变量

ios - Objective-C 在另一个 XiB 中使用自定义 XiB

ios - 无法在 Xcode 6 中构建 iOS 应用程序