对象的retainCount有时候并不能真正返回对象的被引用次数。
参考:http://blog.csdn.net/wzzvictory/article/details/9626017
http://blog.csdn.net/ahopedog/article/details/7868429
1.
苹果对retainCount方法的描述如下:
retainCount
Do not use this method. (required)
Special Considerations
This method is of no value in debugging memory management issues.Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.
在该方法的特别说明部分苹果解释道:
调试内存管理问题时调用该方法是没有任何意义的。因为保不准Cocoa framework中的其它对象会retain我们的目标对象,还有对于autorelease pool中的延迟释放对象,调用这个方法也得不到有用的信息。
2.
Typically there should be no reason to explicitly ask an object what its retain count is (see retainCount). The result is often misleading, as you may be unaware of what framework objects have retained an object in which you are interested. In debugging memory management issues, you should be concerned only with ensuring that your code adheres to the ownership rules.
原文:http://stackoverflow.com/questions/2640568/objectivec-how-to-get-the-reference-count-of-an-nsobject
翻译:通 常我们没有必要去特地查询一个对象的retain count是多少。查询的结果常常会出乎意料。你不清楚framework里面的其他对象对你感兴趣的这个对象进行了多少retain操作。在debug 内存管理的问题时候,你只要关注保证你的代码符合所有者规则即可。