对象的保留计数永远不会低于1,尽管故意过度释放 [英] 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];
看来我可以得到的最低值是r = 1故意添加额外的释放调用
It seems that the lowest value I can get is "r = 1", even if I deliberately add extra "release" calls
[aObject release];
即使我试图将release和NSLog测试代码放在对象的 dealloc
方法。
The "r = 1" limit holds even if I try to put the "release" and "NSLog" test codes in the object's dealloc
method.
Cocoa运行时似乎在使用EXC_BAD_ACCESS崩溃之前忽略了我的额外版本,直到r = (没有GC)。
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).
我唯一的解释(一个猜测)是我们需要r> = 1来访问对象。 Cocoa运行时试图避免让任何对象的保留计数过早地变为0。
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?
推荐答案
当对象的保留计数即将达到0时它的保留计数是1,并且释放已经被再次调用),它被释放而不是打扰做最后的减少。
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.
这篇关于对象的保留计数永远不会低于1,尽管故意过度释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!