Now I have a ClassA : NSObject, and then in the viewcontroller viewDidLoad, look at the under code:
- (void)viewDidLoad {
ClassA *a = [[ClassA alloc] init];
NSLog(@"a retainCount = %d", [a retainCount]);
ClassA *b = a;
NSLog(@"a retainCount = %d b retainCount= %d ", [a retainCount],[b retainCount]);
[a release];
NSLog(@"a retainCount = %d b retainCount= %d", [a retainCount],[b retainCount]);
[super viewDidLoad];
}
The console output looks like:
2012-11-02 14:43:35.437 RetainCountTest[1043:207] a retainCount = 1
2012-11-02 14:43:35.439 RetainCountTest[1043:207] a retainCount = 1 b retainCount= 1
2012-11-02 14:43:35.439 RetainCountTest[1043:207] a retainCount = 1 b retainCount= 1
I don’t understand that when I call [a release], why the [a retainCount]== 1?
You should never pay attention to retainCount’s. They are at best confusing, and at worst misleading. One should just make sure that they follow memory management rules of retain/release correctly, and forget about retainCounts.
From documentation..
EDIT : Suggested Reading
When to use -retainCount?
EDIT : After seeing OP’s comment
From Cocoa Core Memory Management rules
If one read this, he/she might think, Oh retainCount is godsent, and I can see the complete alloc/retain/release cycle of an object just using an NSLog statement. But it doesn’t actually works that way. You cannot say, you have sole ownership of object you create. That object might be retained by any other framework object. And by releasing you are just relinquishing your ownership. The object will get released only after all other objects remove their reference.
I don’t know why it remains in public API.