Objective-C保留dealloc中的计数 [英] Objective-C retain counts in dealloc
问题描述
我在这里看到一些奇怪的东西,我在应用程序中的各种 dealloc
方法中设置了断点,在检查时,对象的保留计数 self
从1到0.当调用 dealloc
时,对象的保留计数是否已经设置为0?
我在控制台使用 print(int)[self retainCount]
>
0似乎只出现在我的 NSOperation
的 dealloc
任何想法为什么会是这样?
$ b
对象的保留计数在 -dealloc
中无关紧要。
引用计数的正常实现使用大于零的值的外部引用计数 - 参见 NSIncrementExtraRefCount()
和 NSDecrementExtraRefCountWasZero()
。当extraRefCount计数为零时,refCount为1。当调用 NSDecrementExtraRefCountWasZero()
并且extraRefCount已经为零时,它返回 YES
和 dealloc
被调用。除非处理 NSDecrementExtraRefCountWasZero()
的返回值,因此无法区分refCount和refCount零。
NSOperation
获取零refCount表明它不使用正常的机制。
I'm seeing something fairly strange here, I've got breakpoints set in various dealloc
methods in my app, and on inspection, the retain counts of the object self
varies from 1 to 0. When dealloc
is called, will the retain count of the object be set to 0 already?
I'm using print (int) [self retainCount]
in the console to test this.
The 0's seem to only appear in the dealloc
of my NSOperation
's that are being run in an NSOperationQueue
.
Any idea why this is?
The retain count of your object doesn’t matter in -dealloc
. For practical purposes, it’s undefined.
The normal implementation of reference counting uses an external reference count for values greater than zero – see NSIncrementExtraRefCount()
and NSDecrementExtraRefCountWasZero()
. When the extraRefCount count is zero, the refCount is one. When NSDecrementExtraRefCountWasZero()
is called and the extraRefCount is already zero, it returns YES
and -dealloc
is called. Except when dealing with the return value of NSDecrementExtraRefCountWasZero()
there is no way to distinguish a refCount of one from a refCount of zero.
That NSOperation
gets a zero refCount suggests it’s not using the normal mechanism.
这篇关于Objective-C保留dealloc中的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!