1

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?

laalto
  • 150,114
  • 66
  • 286
  • 303
Carusd Ray
  • 993
  • 7
  • 11
  • possible duplicate of [Objective C NSString* property retain count oddity](http://stackoverflow.com/questions/403112/objective-c-nsstring-property-retain-count-oddity) – Nikolai Ruhe Mar 07 '11 at 14:39

2 Answers2

6

Whilst not technically a duplicate of the question Nikolai linked to, the same thing applies:

Don't use the retainCount property

It's possibly the worst thing Apple ever put into NSObject, because it's so nicely named it tricks you into thinking it's actually useful.

See the question you were previously linked to - Objective C NSString* property retain count oddity - but look for the second answer, the highest rated one.

Community
  • 1
  • 1
lxt
  • 31,146
  • 5
  • 78
  • 83
  • @lst Thanks for pointing that out. There are numerous questions on SO like this one (http://stackoverflow.com/questions/1206111 http://stackoverflow.com/questions/656902 http://stackoverflow.com/questions/1181010). Please file a bug at http://bugreport.apple.com/ to either deprecate retainCount or to update the documentation. – Nikolai Ruhe Mar 07 '11 at 14:49
4

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:

-(void) connectionDidFinishLoading:(NSURLConnection*) connection

OR

-(void) connection:(NSURLConnection*) connection didFailWithError:(NSError*) error

From your example:

. . .
NSURLConnection *_conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
. . .
[_conn release];

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:

NSString* str = @"12345"; //has 2147483647 retain count.
Max
  • 16,679
  • 4
  • 44
  • 57
  • so you are the guy that will tell me why `[NSNumber numberWithInt:1]` has a retainCount of 7 but `[NSNumber numberWithInt:2]` has a retainCount of 3 and what I could do with this information. – Matthias Bauch Mar 07 '11 at 15:19
  • 1
    @fluchtpunkt for the same reason as NSURLConnection: you don't know how many times [NSNumber numberWithInt:1] was called in iOS internals. There is no reason to hold 7 exact copies of NSNumber=1 in memory so this value is just retained. BTW: the retain count for me in one project is 9, and in another is 11. – Max Mar 07 '11 at 15:23
  • and that's why the retainCount information is completely useless. It can have arbitrary values. – Matthias Bauch Mar 07 '11 at 15:23
  • for example [[NSNumber numberWithInt:100] retainCount] returns 1 that proves my thought (maybe for you the number is different). – Max Mar 07 '11 at 15:24
  • @fluchtpunkt well, should assume that I don't rely on it much, however for my custom objects that inherit from NSObject this value is precise. – Max Mar 07 '11 at 15:27
  • If you understand how `retainCount` works, you know to never rely upon it beyond method of absolute last resort. The only time you can *know* it is totally precise in your objects is if you never pass your objects to System API, including use of KVO, KVC and the like. – bbum Mar 07 '11 at 17:26