When to use -retainCount?

前端 未结 11 1334
借酒劲吻你
借酒劲吻你 2020-11-21 06:49

I would like to know in what situation did you use -retainCount so far, and eventually the problems that can happen using it.

Thanks.

相关标签:
11条回答
  • 2020-11-21 06:53

    What problems can you get from using it? All it does is return the retain count of the object. I have never called it and can't think of any reason that I would. I have overridden it in singletons to make sure they aren't deallocated though.

    0 讨论(0)
  • 2020-11-21 06:53

    The examples used in Dave's post are NSNumber and NSStrings...so, if you use some other classes, such as UIViews, I'm sure you will get the correct answer(The retain count depends on the implementation, and it's predictable).

    0 讨论(0)
  • 2020-11-21 07:01

    NEVER!

    Seriously. Just don't do it.

    Just follow the Memory Management Guidelines and only release what you alloc, new or copy (or anything you called retain upon originally).

    @bbum said it best here on SO, and in even more detail on his blog.

    0 讨论(0)
  • 2020-11-21 07:01

    Of course you should never use the retainCount method in your code, since the meaning of its value depends on how many autoreleases have been applied to the object and that is something you cannot predict. However it is very useful for debugging -- especially when you are hunting down memory leaks in code that calls methods of Appkit objects outside of the main event loop -- and it should not be deprecated.

    In your effort to make your point you seriously overstated the inscrutable nature of the value. It is true that it is not always a reference count. There are some special values that are used for flags, for example to indicate that an object should never be deallocated. A number like 1152921504606846975 looks very mysterious until you write it in hex and get 0xfffffffffffffff. And 9223372036854775807 is 0x7fffffffffffffff in hex. And it really is not so surprising that someone would choose to use values like these as flags, given that it would take almost 3000 years to get a retainCount as high as the larger number, assuming you incremented the retainCount 100,000,000 times per second.

    0 讨论(0)
  • 2020-11-21 07:04

    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:

    • You'd think that [NSNumber numberWithInt:1] would have a retainCount of 1. It doesn't. It's 2.
    • You'd think that @"Foo" would have a retainCount of 1. It doesn't. It's 1152921504606846975.
    • You'd think that [NSString stringWithString:@"Foo"] would have a retainCount of 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's retainCount is 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 -retainCount be deprecated. The more people that ask for it, the better.

    edit #2

    As an update,[NSNumber numberWithInt:1] now has a retainCount of 9223372036854775807. If your code was expecting it to be 2, your code has now broken.

    0 讨论(0)
  • 2020-11-21 07:07

    Autoreleased objects are one case where checking -retainCount is uninformative and potentially misleading. The retain count tells you nothing about how many times -autorelease has been called on an object and therefore how many time it will be released when the current autorelease pool drains.

    0 讨论(0)
提交回复
热议问题