Rafer
Rafer

Reputation: 1170

Why is the retainCount still 1 after [object release]?

NSLog(@"first:%u",[object retainCount]);
[object release];
NSLog(@"second:%u",[object retainCount]);

Output:

first:1
second:1

Why doesn't the object get released?

Upvotes: 4

Views: 1007

Answers (5)

DarkMatter
DarkMatter

Reputation: 306

I agree with the other comments about not using retainCount to get a reliable count.

EDIT: Ignore my stupidity below... :)

However, I've observed that setting the corresponding property to nil...

self.object = nil;

the retainCount does tend to be decremented immediately.

Upvotes: -1

bbum
bbum

Reputation: 162712

Divide any number by zero and you will find the meaning of "object with retain count of zero".

Upvotes: 1

Georg Schölly
Georg Schölly

Reputation: 126085

First, retainCount doesn't give you a number you can use. It's meaningless.

Second, the reason the retainCount is 0 is probably that you try to work with an object that doesn't exist anymore. You're lucky your application doesn't crash, because your accessing invalid memory. Decreasing the retainCount just before deallocating an object is unnecessary, therefore Apple doesn't do it, probably.

Upvotes: 1

Guy Ephraim
Guy Ephraim

Reputation: 3520

a Quote from NSObject reference on retainCount method

This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

Upvotes: 14

user189804
user189804

Reputation:

Object can be released but not when you think it will be. Basically, don't look at retainCount. It may not change until the next runloop or at all, it's an implementation detail. You will get a sense for when you need to release and when you don't with experience but until then rely on the clang analyzer.

Upvotes: 4

Related Questions