使用ARC + NSZombieEnabled时,为什么没有释放对象 [英] Why is object not dealloc'ed when using ARC + NSZombieEnabled

查看:125
本文介绍了使用ARC + NSZombieEnabled时,为什么没有释放对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的应用程序转换为ARC,并注意到在我的一个视图控制器中分配的对象在该视图控制器被解除分配时没有被释放。花了一段时间才弄清楚原因。我在调试时为我的项目启用了Zombie Objects,结果证明这是原因。考虑以下应用程序逻辑:

I converted my app to ARC and noticed that an object alloc'ed in one of my view controllers was not being dealloc'ed when that view controller was dealloc'ed. It took a while to figure out why. I have Enable Zombie Objects on for my project while debugging and this turned out to be the cause. Consider the following app logic:

1)用户在 RootViewController 中调用导致的操作SecondaryViewController 将通过 presentModalViewController创建并呈现:动画

1) Users invokes action in RootViewController that causes a SecondaryViewController to be created and presented via presentModalViewController:animated.

2) SecondaryViewController 包含一个 ActionsController ,它是一个 NSObject 子类。

2) SecondaryViewController contains an ActionsController that is an NSObject subclass.

3) ActionsController 在初始化时通过 NSNotificationCenter 观察通知并且当它被解除分类时停止观察。

3) ActionsController observes a notification via NSNotificationCenter when it is initialized and stops observing when it is dealloc'ed.

4)用户解除 SecondaryViewController 返回 RootViewController

关闭启用僵尸对象,上面的工作正常,所有对象都被取消分配。即使 SecondaryViewController 已取消分配,也不会取消分配 ActionsController 上的启用僵尸对象。

With Enable Zombie Objects turned off, the above works fine, all objects are deallocated. With Enable Zombie Objects on ActionsController is not deallocated even though SecondaryViewController is deallocated.

这导致我的应用程序出现问题b / c NSNotificationCenter 继续向 ActionsController 发送通知结果处理程序导致应用程序崩溃。

This caused problems in my app b/c NSNotificationCenter continues to send notifications to ActionsController and the resulting handlers cause the app to crash.

我在 https创建了一个简单的应用程序://github.com/xjones/XJARCTestApp 的。打开/关闭启用僵尸对象的控制台日志以验证这一点。

I created a simple app illustrating this at https://github.com/xjones/XJARCTestApp. Look at the console log with Enable Zombie Objects on/off to verify this.

问题(S)


  1. 这是启用僵尸对象的正确行为吗?

  2. 我应该如何实现这种类型的逻辑来消除这个问题。我想继续使用Enable Zombie Objects。

编辑#1:根据Kevin的建议我已将此提交给Apple和openradar在 http://openradar.appspot.com/10537635

EDIT #1: per Kevin's suggestion I've submitted this to Apple and openradar at http://openradar.appspot.com/10537635.

编辑#2:澄清一个好的答案

首先,我是一位经验丰富的iOS开发人员,我完全理解ARC,僵尸物品等等。如果我遗漏了某些东西,当然,我很欣赏任何照明。

First, I'm an experienced iOS developer and I fully understand ARC, zombie objects, etc. If I'm missing something, of course, I appreciate any illumination.

其次,这个特定崩溃的解决办法确实是在 secondaryViewController 被取消分配时删除 actionsController 作为观察者。我还发现如果我明确设置 actionsController = nil secondaryViewController 被解除分类时,它将被解除分配。这两个都不是很好的解决方法b / c它们实际上要求你使用ARC但是代码好像你没有使用ARC(例如在dealloc中明确地使用nil iVars)。一个特定的解决方案也无法确定何时这会成为其他控制器的问题,因此开发人员确定地知道何时/如何解决此问题。

Second, it is true that a workaround for this specific crash is to remove actionsController as an observer when secondaryViewController is deallocated. I have also found that if I explicitly set actionsController = nil when secondaryViewController is dealloc'ed it will be dealloc'ed. Both of these are not great workaround b/c they effectively require you to use ARC but code as if you are not using ARC (e.g. nil iVars explicitly in dealloc). A specific solution also doesn't help identify when this would be an issue in other controllers so developers know deterministically when/how to workaround this issue.

一个好的答案可以解释在使用ARC + NSZombieEnabled时,如何确定性地知道你需要对某个对象做一些特殊的事情,这样它就可以解决这个具体的例子,并且通常也适用于整个项目,而不会留下其他类似问题的可能性。

A good answer would explain how to deterministically know that you need to do something special wrt an object when using ARC + NSZombieEnabled so it would solve this specific example and also apply generally to a project as a whole w/o leaving the potential for other similar problems.

完全有可能没有好的答案,因为这可能是XCode中的一个错误。

It is entirely possible that a good answer doesn't exist as this may be a bug in XCode.

谢谢所有!

推荐答案

原来这是一个iOS错误。 Apple已与我联系并表示他们已在iOS 6中修复此问题。

Turns out it is an iOS bug. Apple has contacted me and indicated they've fixed this in iOS 6.

这篇关于使用ARC + NSZombieEnabled时,为什么没有释放对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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