There aren't many setters that don't either copy or retain a variable being passed to it, lest the memory of said variable be re-allocated to something else when its retain count reaches zero.
However, the answer is YES, it does. A little bit of test code shows the delegate's retain count go up:
NSLog(@"Retain count before: %d", [self retainCount]);
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
NSURLConnection* conn = [NSURLConnection connectionWithRequest:request delegate:self];
NSLog(@"Retain count after: %d", [self retainCount]);
which produces in the log:
Running…
2009-07-09 02:13:40.516 delegateRetain[45123:a0f] Retain count before: 1
2009-07-09 02:13:40.525 delegateRetain[45123:a0f] Retain count after: 2
Debugger stopped.
So you can see pretty clearly that in connectionWithRequest:delegate:
"self" is indeed having its retain count increased +1. If you're feeling brave and want to mess with the EXC_BAD_ACCESS gods, add in
[conn dealloc];
NSLog(@"Retain count after dealloc: %d", [self retainCount]);
which will print out "1" again, showing a post dealloc decrement. However, you'll get a nice Program received signal: “EXC_BAD_ACCESS”.
because the NSAutoreleasePool
will try to release the connection and it will be gone ;)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…