26

Is there a way to fetch the current reference count for an NSObject (i.e. NSString)?

zoul
  • 102,279
  • 44
  • 260
  • 354
zer0stimulus
  • 22,306
  • 30
  • 110
  • 141

3 Answers3

29

retainCount

But you should consider using CFGetRetainCount instead

Alexander Bekert
  • 617
  • 8
  • 18
Ben S
  • 68,394
  • 30
  • 171
  • 212
  • 1
    This method is discouraged to be used and even not guaranteed to return correct number. – eonil Aug 04 '12 at 09:12
24

As @Ben S said, it's the retainCount method. However, you're asking the wrong question, because:

Important: Typically there should be no reason to explicitly ask an object what its retain count is (see retainCount). The result is often misleading, as you may be unaware of what framework objects have retained an object in which you are interested. In debugging memory management issues, you should be concerned only with ensuring that your code adheres to the ownership rules.

So here's the real question: why do you need to know?

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Somewhere in my ObjectiveC code a release was invoked on an already-released object. For some reason gdb will not break at the specific location where the memory access violation is occuring. I have a good guess where the problem resides but would like to verify my assumption by printing the refcount of the suspect object. – zer0stimulus Apr 14 '10 at 20:04
  • 10
    Put a break point on the dealloc method. Also, check this link out: http://www.cocoadev.com/index.pl?NSZombieEnabled – JeremyP Apr 14 '10 at 20:10
  • 5
    Better yet, use Instruments's Zombies template. – Peter Hosey Apr 14 '10 at 20:18
  • 1
    If you're on 10.6, "Build and Analyze" in the Build menu may help point you at the problematic code. – Isaac Apr 15 '10 at 02:27
  • 87
    You want to know, because you're trying to debug your application. You can't tell someone, "you don't need to know, because you should just do it right, in which case you won't care". Maybe they shouldn't need the code in their production release, but that's not the same thing as not needing to know the ref count, as a tool to debug why you're having problems. Bad answer. – Nate Aug 04 '10 at 01:34
  • 10
    Don't get me wrong. Instruments is great. But, it has limitations, and there's no reason a developer should be limited to just using Instruments. What if the problem doesn't crop up in your desktop test environment? What if you want to deploy devices to the field, and use logging to give you something to analyze later? Or maybe there's something location-based about your app, or about the problem code, that requires you taking the device out of your office. You may not be able to bring your development workstation into the field. The poster doesn't need to justify asking the question. – Nate Apr 21 '11 at 09:40
  • 1
    I find it handy to check the reference count sometimes. Notably, it helps me understand "what framework objects have retained an object in which [i am] interested". And it helped me understand reference counting. –  Dec 12 '11 at 08:11
  • The absolute `retainCount` of an object is useless for a variety of reasons. What little information it does provide, doesn't carry enough data to be interpreted usefully; it just ends up creating deeper questions that you have to use different tools to answer. – bbum Sep 11 '12 at 14:59
  • I ended up here, but realized this is my real question: http://stackoverflow.com/questions/9336288/nsarray-of-weak-references-to-objects-under-arc – paulmelnikow Sep 21 '12 at 16:17
  • I hate when Apple documentation says - `..there's no reason to call the method...` ! Why did you put it there if there's no reason? – Nenad Feb 03 '15 at 19:26
  • ARC forbids explicit message send to retain Count. Is there a way to find it out in ARC? – Frane Poljak Mar 21 '16 at 18:09
  • Anybody who thinks there's no reason to look at the reference count of an object has never debugged a complex memory management problem in their life. – Glenn Maynard Jan 31 '23 at 07:42
10

using CFGetRetainCount function

Example :

print(CFGetRetainCount(object))

Read more here.

Cache Staheli
  • 3,510
  • 7
  • 32
  • 51
Tritmm
  • 1,972
  • 18
  • 14