Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 569
Rafer Avatar asked Dec 02 '22 03:12

Rafer


2 Answers

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.

like image 190
Guy Ephraim Avatar answered Dec 19 '22 09:12

Guy Ephraim


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.

like image 29
Adam Eberbach Avatar answered Dec 19 '22 07:12

Adam Eberbach