When to use -retainCount?

前端 未结 11 1335
借酒劲吻你
借酒劲吻你 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条回答
  • 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.

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

    Take a look at the Apple documentation on NSObject, it pretty much covers your question: NSObject retainCount

    In short, retainCount is probably useless to you unless you've implemented your own reference counting system (and I can almost guarantee you won't have).

    In Apple's own words, retainCount is "typically of no value in debugging memory management issues".

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

    You should never use it in your code, but it could definitely help when debugging

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

    Never use the -retainCount in your code. However if you use, you will never see it returns zero. Think about why. :-)

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

    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.

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