如何在 UIViewController 的 didReceiveMemoryWarning 中获取 EXC_BAD_ACCESS?

发布于 2024-10-17 22:29:27 字数 409 浏览 1 评论 0原文

我正在 UIViewController 的子类中实现 didReceiveMemoryWarning。我的代码如下所示:

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [self cleanUp];
}

当我的应用程序实际收到内存警告时,应用程序因 [self cleanUp] 行上的 EXC_BAD_ACCESS 崩溃(确实存在的方法)。怎么会发生这种事?据我了解,框架调用了 didReceiveMemoryWarning 方法,然后在尝试执行 [self cleanUp] 之前释放了我的类。为什么会出现这种情况呢?我怎样才能防止这种情况发生?

I'm implementing didReceiveMemoryWarning in a subclass of UIViewController. My code looks something like the following:

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [self cleanUp];
}

When my app actually received a memory warning, the app crashed with an EXC_BAD_ACCESS on the [self cleanUp] line (a method that does exist). How could this happen? As I understand it, the framework called the didReceiveMemoryWarning method, then released my class before it attempted to execute [self cleanUp]. Why would this happen? How can I prevent this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏别ゝ 2024-10-24 22:29:27

你在视图控制器中做了什么不寻常的事情吗?崩溃时self的值是多少?在调试(优化关闭)和发布版本中都会发生这种情况吗?

尝试使用 NSZombieEnabled。如果这是不平衡保留/释放的问题,那应该可以帮助您找到它。

Are you doing anything unusual in your view controller? What is the value of self when the crash occurs? Does it happen in both debug (optimization off) and release builds?

Try running with NSZombieEnabled. If this is a problem with unbalanced retain/release, that should help you find it.

清秋悲枫 2024-10-24 22:29:27

崩溃实际上发生在 -(void)cleanUp 方法内部,尽管 Xcode 指向调用 [self cleanUp] 的行。在 -(void)cleanUp 内,代码正在访问已释放的数组中的元素,因此出现 EXC_BAD_ACCESS。感谢大家的有用建议。

The crash was actually happening inside the -(void)cleanUp method, though Xcode was pointing to the line that called [self cleanUp]. Inside the -(void)cleanUp the code was accessing elements in an array that were already released, hence the EXC_BAD_ACCESS. Thanks to all for the helpful suggestions.

后eg是否自 2024-10-24 22:29:27

疯狂猜测:先调用[self cleanup],然后调用super。
如果有帮助的话,您还可以在 sim 中模拟内存警告。

Wild guess: call [self cleanup] first, then call super.
You can also simulate memory warnings in the sim, if that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文