Obj-C 消息发送到已释放对象 (EXEC_BAD_ACCESS),使用 iOS5、ARC
我正在使用 ARC 开发一个 iOS5 应用程序,我开始遇到一些随机的 EXEC_BAD_ACCESS 崩溃,我无法弄清楚。我所说的随机是指它是非常不可预测的:有时崩溃可能需要很长时间,有时很短。并且也没有一个特定的按钮/表格单元格/等。触发崩溃。每次用户交互都可能使应用程序崩溃,但您不能重复崩溃。
我尝试打开 NSZombie 以及一些 malloc 调试器工具。在 Instruments 中,崩溃错误如下:一条 Objective-C 消息已发送到地址为 0x10bd1b40 的已释放对象(僵尸)。引用计数日志的最后一部分如下所示:
475 CoursesFirstViewController Release 2 02:23.253.631 0 UIKit -[UINibDecoder finishDecoding]
476 CoursesFirstViewController Release 1 02:23.253.838 0 Foundation -[NSAutoreleasePool drain]
477 CoursesFirstViewController Zombie -1 02:35.752.420 0 Foundation objectHash
2、1、-1 是引用计数。我不知道为什么它会跳过 0 并下降到 -1,从而使程序崩溃(除了最后一个条目之外的所有条目都有连续的引用计数)。我也不知道 objectHash 是什么。
我的应用程序包含多种功能,可在主屏幕上通过图标进行访问。 CoursesFirstViewController 就是其中一个功能。始终是 CoursesFirstViewController 和 objectHash 导致应用程序崩溃,即使我在其他地方。 (上面的日志就是这种情况:我在 02:23 退出 CoursesFirstViewController (从而返回到我的应用程序的主屏幕),但 12 秒后,当我在使用其他功能时,应用程序崩溃了)我只需要进入CourseFirstViewController,摆弄一下,然后去其他地方继续使用该应用程序,一段时间后它就会崩溃。
我现在真的对这个问题很生气。我已经搜索了SO和Google很长一段时间但找不到解决方案。任何帮助将不胜感激。谢谢!!
I'm developing an iOS5 App using ARC, and I started to get some random EXEC_BAD_ACCESS crashes that I cannot figure out.. By random I mean it is very unpredictable: sometimes it may take a long time to crash, sometimes short. and there is also no one specific button/tablecell/etc. that trigger the crash. Every user interaction can possibly crash the app, but you cannot repeat the crash.
I have tried to turn on NSZombie and also some malloc debugger tools. In Instruments the crash error reads: An Objective-C message was sent to a deallocated object (zombie) at address: 0x10bd1b40. And the last part of the reference count log looks like this:
475 CoursesFirstViewController Release 2 02:23.253.631 0 UIKit -[UINibDecoder finishDecoding]
476 CoursesFirstViewController Release 1 02:23.253.838 0 Foundation -[NSAutoreleasePool drain]
477 CoursesFirstViewController Zombie -1 02:35.752.420 0 Foundation objectHash
The 2, 1, -1 thing are the reference counts. I have no idea why it skips 0 and drop to -1, crashing the program (all entries but the last have continuous ref counts). Also I have no idea what objectHash is.
My app comprises of multiple functions, accessible as icons on my main screen. CoursesFirstViewController is one of the functions. Always, it is CoursesFirstViewController and objectHash that crash the app, even if I am at somewhere else. (This is the case for the log above: I went out of CoursesFirstViewController (thus returning to the main screen of my app) at 02:23, but after 12 sec, when I was in some other function, the app crashed) I only need to enter CourseFirstViewController, mess around with it for a bit, and then go elsewhere to continue using the app, and after a while it will just crash.
I'm really mad of this problem now. I have searched SO and Google for quite a while but cannot find a solution. Any help will be greatly greatly appreciated. Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这实际上有点用。这告诉我们一个集合,可能是一个
NSDictionary
,但也可能是一个NSSet
正在被销毁。从您之前的信息来看,它似乎是在 nib 实例化过程中创建的自动释放集合(因此可能是CoursesFirstViewController
的 ivar)。无论如何,考虑到这些症状,我都会去那里寻找,但这次事故似乎证实了这一点。一般建议是审核任何
__bridge
强制转换或unsafe_unretained
属性。另一个预感是您以违反 Cocoa 内存管理的方式命名了一个方法。最可能的错误命名是您有一个以
new
或copy
开头的属性。如果 ARC 代码调用错误命名的非 ARC 代码,这肯定会成为一个问题。This is actually somewhat useful. This is telling us that a collection, probably an
NSDictionary
, but possibly anNSSet
is being destroyed. From your earlier information it seems to be an autoreleased collection that is created during the nib instantiation process (so probably an ivar ofCoursesFirstViewController
). That's where I'd be looking anyway given the symptoms, but the crash seems to confirm it.General recommendations are auditing any
__bridge
casts orunsafe_unretained
properties.Another hunch is that you've named a method in a way that violates Cocoa memory management. The most likely misnaming would be that you have a property that starts with
new
orcopy
. This would definitely be an issue if you have ARC code call misnamed non-ARC code.我怀疑您的一项属性在本应为“强”的情况下被标记为“弱”,并且超出了范围并在您访问它之前被释放。在这种情况下,ARC 无法拯救您,您将得到上述随机崩溃行为。
如果你没有运气找到它,我会求助于对你的项目中的“弱”属性进行全局搜索,并仔细检查每个属性以确保它不是你期望的东西。这很乏味,但说实话,做起来并不需要那么长时间,而且有时会出现不止一个错误。
I suspect one of your properties is marked "weak" when it should be "strong" and is going out of scope and getting released before you access it. ARC can't save you in that case and you will get the above random crash behavior.
If you don't have any luck finding it I would resort to doing a global search for "weak" properties on your project and double-check each one to make sure it isn't something you expect to hang around. It's tedious but honestly doesn't take that long to do and sometimes turns up more than the one bug.
我从您的 Instruments 输出中了解到,僵尸对象的类型为
CoursesFirstViewController
。我们经常使用视图控制器作为其他对象的委托,并且大多数具有委托的对象不会保留其委托。我假设您没有使用任何
unsafe_unretained
属性或变量,或任何retain
类型属性。因此,我假设您的任何对象都没有保留对CoursesFirstViewController
僵尸的悬空引用。由于系统库必须使用非 ARC 代码,因此它们不使用 ARC 的弱引用。因此,如果某个系统对象将
CoursesFirstViewController
作为其委托,并且CoursesFirstViewController
被销毁,但系统对象没有被销毁,那么系统对象将留下一个对被摧毁的物体。因此,请检查您是否使用
CoursesFirstViewController
作为任何系统对象的委托。如果是这样,您确定这些对象的寿命不会比CoursesFirstViewController
长吗?I gather from your Instruments output that the zombie object is of type
CoursesFirstViewController
. We often use a view controller as the delegate for some other object, and most objects that have delegates do not retain their delegates.I am assuming that you aren't using any
unsafe_unretained
properties or variables, or anyretain
-type properties. So I assume that none of your objects is keeping a dangling reference to theCoursesFirstViewController
zombie.Since the system libraries have to work with non-ARC code, they do not use ARC's weak references. So if some system object has a
CoursesFirstViewController
as its delegate, and theCoursesFirstViewController
is destroyed but the system object is not, then the system object is left with a dangling reference to the destroyed object.So, check whether you're using the
CoursesFirstViewController
as the delegate for any system objects. If so, are you sure those objects don't outlive theCoursesFirstViewController
?