问题描述
另一个难题.我有一个NSArray(iTours),其中包含3个对象,保留计数为1.
Another puzzler. I have an NSArray (iTours) containing 3 Objects and a retain count of 1.
此数组在对象dealloc方法中释放,如下所示:
This Array is released in the object dealloc method as follows:
- (void)dealloc {
NSLog(@"retain count für iTouren: %d", [iTours retainCount]);
[iTours release]; // triggers EXC_BAD_ACCESS
[super dealloc];
}
控制台窗口显示以下消息(没有更多详细信息):
The console window shows the following message (no further details):
有什么线索吗?我想念什么?
any clues what's going on? What did I miss?
推荐答案
很可能是您过度释放了iTouren
,因此对release
的调用导致崩溃.也就是说,在释放包含数组时,iTouren
已被释放;当该包含数组将release
发送到已释放的iTouren
时,您的应用程序将崩溃.
Most likely, you are over-releasing iTouren
and, thus, that call to release
is causing the crash. That is, iTouren
is already deallocated by the time you release the containing array and when that containing array sends release
to the already deallocated iTouren
your app crashes.
(当然,iTours
可能是已经释放的对象.无论如何,这是一个过度释放的问题.)
(Of course, iTours
might be the object that is already deallocated. In any case, it is an over-release problem.)
打开僵尸检测功能,看看是否能解决特定问题.
Turn on zombie detection and see if that barfs up the specific problem.
请注意,retainCount
返回的
数字是无用的
.绝对保留数是实现细节,通常是一个看起来像胡说八道的特定值.Note that the
number returned by retainCount
is useless
. Absolute retain counts are an implementation detail and will often be a specific value that seems like nonsense.在这种情况下,对象的最后一个release
不会减少保留计数.为什么?因为当对象将要被重新分配时,这将是一个浪费的周期. retainCount
不可能返回0,因为根据定义,保留计数为0的对象已经被释放,因此无论如何它不再是可行的消息接收器.
In this case, the final release
of an object does not decrement the retain count. Why? Because that'd be a wasted cycle when the object is about to be deallocated anyway. It would be impossible for retainCount
to return 0 because, by definition, an object with a retain count of 0 has already been deallocated and, thus, is no longer a viable message receiver anyway.
这篇关于NSArray版本因EXC_BAD_ACCESS而崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!