I would like to know in what situation did you use -retainCount so far, and eventually the problems that can happen using it.
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You should never use
-retainCount, because it never tells you anything useful. The implementation of the Foundation and AppKit/UIKit frameworks is opaque; you don’t know what’s being retained, why it’s being retained, who’s retaining it, when it was retained, and so on.For example:
[NSNumber numberWithInt:1]would have aretainCountof 1. It doesn’t. It’s 2.@"Foo"would have aretainCountof 1. It doesn’t. It’s 1152921504606846975.[NSString stringWithString:@"Foo"]would have aretainCountof 1. It doesn’t. Again, it’s 1152921504606846975.Basically, since anything can retain an object (and therefore alter its
retainCount), and since you don’t have the source to most of the code that runs an application, an object’sretainCountis meaningless.If you’re trying to track down why an object isn’t getting deallocated, use the Leaks tool in Instruments. If you’re trying to track down why an object was deallocated too soon, use the Zombies tool in Instruments.
But don’t use
-retainCount. It’s a truly worthless method.edit
Please everyone go to http://bugreport.apple.com and request that
-retainCountbe deprecated. The more people that ask for it, the better.edit #2
As an update,
[NSNumber numberWithInt:1]now has aretainCountof 9223372036854775807. If your code was expecting it to be 2, your code has now broken.