When to use -retainCount?

2018-12-31 00:43发布

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条回答
唯独是你
2楼-- · 2018-12-31 01:35

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.

查看更多
有味是清欢
3楼-- · 2018-12-31 01:41

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.

查看更多
初与友歌
4楼-- · 2018-12-31 01:47

You should not be worrying about memory leaking until your app is up and running and doing something useful.

Once it is, fire up Instruments and use the app and see if memory leaks really happen. In most cases you created an object yourself (thus you own it) and forgot to release it after you were done.

Don't try and optimize your code as you are writing it, your guesses as to what may leak memory or take too long are often wrong when you actually use the app normally.

Do try and write correct code e.g. if you create an object using alloc and such, then make sure you release it properly.

查看更多
浮光初槿花落
5楼-- · 2018-12-31 01:48

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.

查看更多
查无此人
6楼-- · 2018-12-31 01:48

I do find retainCounts very useful when checked using 'Instruments'.

Using the 'allocations' tool, make sure 'Record reference counts' is turned on and you can go into any object and see its retainCount history.

By pairing allocs and releases you can get a good picture of what is going on and often solve those difficult cases where something is not being released.

This has never let me down - including finding bugs in early beta releases of iOS.

查看更多
登录 后发表回答