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()andNSDecrementExtraRefCountWasZero(). When the extraRefCount count is zero, the refCount is one. WhenNSDecrementExtraRefCountWasZero()is called and the extraRefCount is already zero, it returnsYESand-deallocis called. Except when dealing with the return value ofNSDecrementExtraRefCountWasZero()there is no way to distinguish a refCount of one from a refCount of zero.That
NSOperationgets a zero refCount suggests it’s not using the normal mechanism.