Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a NSURLConnection retain its delegate?

Summary of my question: Does NSURLConnection retain its delegate?

Detailed question and scenario:

I have a custom class, called JsonDownloader that is takes in a URL and returns an NSDictionary of the JSON that the URL returns.

On an iPhone app, I do something like this. (the init method kicks off the whole process)

- (void)viewDidLoad {
    JsonDownloder *temp = [[[JsonDownloader alloc] initWithURL:urlString returnDataTo:self]];
    [temp release];
    [super viewDidLoad];
}

When the JsonDownloader is done downloading and parsing, it performs a callback to the returnDataTo: object, in this case, the calling object.

This works just fine. Even if I introduce a 30 second delay in my web servers response, the JsonDownloader still exists and does it's callback correctly.

So my questions is this: What is keeping JsonDownloader way past the end of the event cycle? I am explicitly releasing it.

My hunch is that NSURLConnection must do a retain on its delegate, but I didn't see anything in the documentation. Anyone have an ideas?

like image 594
Ian Avatar asked Jul 09 '09 04:07

Ian


People also ask

Is a delegate retained?

The delegate object is retained by the receiver. This is a rare exception to the memory management rules described in Advanced Memory Management Programming Guide. An instance of CAAnimation should not be set as a delegate of itself.

What is NSURLConnection in swift?

An NSURLConnection object lets you load the contents of a URL by providing a URL request object. The interface for NSURLConnection is sparse, providing only the controls to start and cancel asynchronous loads of a URL request. You perform most of your configuration on the URL request object itself. Note.


3 Answers

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 ;)

like image 92
Dave Martorana Avatar answered Sep 21 '22 03:09

Dave Martorana


Most delegate properties are not retained but assigned to prevent circular references. See this question about that as well.

However, NSUrlConnection does not have a specific delegate property. You have to specify the delegate along with the initialization of the connection. I think that is why it does receive a retain, as Dave Martorana showed.

like image 45
drvdijk Avatar answered Sep 18 '22 03:09

drvdijk


Yes, "The connection retains delegate. It releases delegate when the connection finishes loading, fails, or is canceled," according to the Xcode Documentation for -[NSURLConnection initWithRequest:delegate:] under Special Considerations. See also: NSURLConnection inherent memory leak?

like image 26
ma11hew28 Avatar answered Sep 17 '22 03:09

ma11hew28