tags:

views:

454

answers:

3

What is the correct point at which to release a NSUrlConnection object?

In my program, I alloc a NSUrlConnection, and then initWithRequest to kick off asynchronously.

I am now responsible for releasing the object - when do I/can I release? Immediately if I'm not using it again?

+5  A: 

When you are done with the NSURLConnection. You'll know you're done with it when your NSURLConnection delegate receives - (void)connectionDidFinishLoading:(NSURLConnection *)connection or - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error. Just release it at the end of those two methods.

Gordon Worley
This causes my application to crash with BAD_ACCESS. I believe you can release it straight away as @Alan says.
Nathan Reed
+1  A: 

You don't need to release it until the framework issues you a notification that it's done, in, say, connection:didFailWithError:.

Jed Smith
+3  A: 

Your usage of the NSUrlConnection can be released immediately.

The framework will release when it has finished with it.

Alan