zer0stimulus
zer0stimulus

Reputation: 23606

How to get the reference count of an NSObject?

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

Upvotes: 26

Views: 32321

Answers (3)

Ben S
Ben S

Reputation: 69342

retainCount

But you should consider using CFGetRetainCount instead

Upvotes: 29

Tritmm
Tritmm

Reputation: 2082

using CFGetRetainCount function

Example :

print(CFGetRetainCount(object))

Read more here.

Upvotes: 10

Dave DeLong
Dave DeLong

Reputation: 243146

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?

Upvotes: 24

Related Questions