first look at these code:
NSURL *url = [[NSURL alloc] initWithString:@"lasdhfkjasf"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnection *_conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"aaaaaaaaa %d", [_conn retainCount]);
[url release];
[request release];
[_conn release];
turns out it prints “aaaaaaaaaaaaa 2”,shouldn’t it be 1?Or there are some kind of exception out there.Then I change it :
NSURL *url = [[NSURL alloc] initWithString:@"lasdhfkjasf"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnection *_conn = [[NSURLConnection alloc] init];
NSLog(@"aaaaaaaaa %d", [_conn retainCount]);
[url release];
[request release];
[_conn release];
I don’t know happen in the initWithRequest:delegate: method,has anybody know about it?
Everything is absolutely OK here: NSURLConnection must retain itself to be sure that it can deliver data to the delegate (and to do that it must not be deallocated). If there is no delegate, then nobody listens to that connection and there is no reason to perform anything, so it doesn’t retain itself. Connection then releases itself after:
OR
From your example:
if retainCount was 1, then after
[_conn release]the object would be deallocated immediately and there would be no loading at all.To all of you, who says that retainCount works incorrectly: you just don’t know how it works. As for NSString ‘oddity’: this is not oddity, this is just performance optimization. 2147483647 retain count means that object is constant in memory (and is deleted when app terminates). This is done when the value is known during compilation: