开发者

Retain count in objective C return -1

I'm new to objective C, I have a NSMutableArray with 3 objects in it, then I try to print the retainCount of the arra开发者_Go百科y. Why the final retainCount return -1? Thanks

NSLog(@"myArray has retain count of %d", [myArray retainCount]);    
[myArray release];
NSLog(@"myArray has retain count of %d", [myArray retainCount]);

Result from console:

2010-10-17 11:58:06.407 TestRetainCount [527:a0f] myArray has retain count of 1
2010-10-17 11:58:06.407 TestRetainCount [527:a0f] myArray has retain count of -1


After an object has been deallocated (which may happen after a release), you can no longer rely on its data being intact. You're trying to trust the retain count after it has become invalid.

On a general note, don't use the retain count. Ever. Use the rules in the memory management programming guide, and you'll always get the reference counting correct.


Graham Lee answered the question specific to your example.

not specific to your example, but to your question (subject):

UINT_MAX is often used to denote an object which uses no reference counting (e.g., is never deallocated, such as a singleton), or a custom reference counting implementation.


Can't rely on an accurate retainCount because of the timing autoreleased objects. That said, count your alloc/init, new, retains, etc... and match with corresponding release.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜