objective-ccocoamemory-managementretaincount

Objects' retain counts never go below 1 despite deliberately overreleasing


I am checking on the retain count of some objects

NSLog(@"r = %d", [aObject retainCount];

It seems that the lowest value I can get is "r = 1", even if I deliberately add extra "release" calls

[aObject release];

The "r = 1" limit holds even if I try to put the "release" and "NSLog" test codes in the object's dealloc method.

The Cocoa run-time just seems to neglect my extra releases up to "r = 1" before crashing with an "EXC_BAD_ACCESS" at the very end of the sample program (without GC).

My only explanation (a guess) is that we need r >= 1 for the object to be accessed. And the Cocoa run-time just tries to refrain from letting any object's retain count from getting to 0 prematurely.

Can somebody confirm or correct me if I am wrong?


Solution

  • When the retain count of an object is about to reach 0 (i.e. its retain count is 1, and release has been called again), it's deallocated instead of bothering to do the final decrement.