When passing a delegate to the a NSUrlConnection
object like so:
[[NSURLConnection alloc] initWithRequest:request delegate:handler];
when should you call release on the delegate? Should it be in connectionDidFinishLoading
? If so, I keep getting exec_bad_access
. I'm seeing that my delegates are leaking through instruments.
Thanks
NSURLConnection
delegate is retained.Use the code in
ViewDidLoad
with non-ARC environment.the object handler is used to implement connectionDidFinishLoading didReceiveData etc. I make a lot of calls to a number of web services and instead of creating one object for each, I have a central class for all that stuff:
so the implementation of that method creates the delegate to pass:
what I am seeing in Instruments is that there is a memory leak on the SearchDelegate... so I think it is actually being retain'ed.
Tinkering a bit I changed my sendRequestToUrlMethod to have this:
and this seems to have gotten ridden of memory leak being reported in Instruments.
Taken from my blog post here: http://i.ndigo.com.br/2012/01/releasing-nsurlconnection-and-its-delegate/
You will have to pay extra attention to the delegate object as, for
NSURLConnection
, there is a special consideration for the delegate: it is always retained.http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/doc/uid/20001697-BAJDDIDG
So, taking that into consideration, you have several options to ensure that your delegate will be released correctly and I will try to explain 2 simple ones.
The first, and most commonly used, is to use the same class that initialize the NSURLConnection as the delegate.
By doing that, your class the retain count would be increased by 1 when the connection starts and then would de reduced by 1 after the connection finishes loading, fails, or is canceled, resulting in no memory leaks.
The second option, the one that you are trying to do, is to use another object to handle all connection calls. This works fine as well, but you will need extra attention with memory. One simple thing you could do to solve your problem is to initialize the connection with an autorelease object.
OR you could release your handler right after creating the connection (as it will be already retained by the connection)
Both ways will leave the handler object ownership only with the connection class, which will release the handler object right after it finishes loading, fails, or is canceled, once again resulting in no memory leaks.
EDIT: By doing any of the options above you don't have to worry about releasing the delegate (but you still have to release the connection) in
connection:DidFinishLoading
andconnection:didFailWithError
methods.It will depend on what object
handler
is and how you use it. For example, I usually useself
as my delegate:[[NSURLConnection alloc] initWithRequest:request delegate:self];
I don't need to call release on
self
because delegates are not retained andself
will be released by another object.If
handler
is a new object, then you will have to release it (and connectionDidFinishLoading: should be ok, unless you need to use thehandler
object for something else).Are you familiar with the rules for memory management in Cocoa?
Can you give a better picture of what object
handler
is and how you're using it?Your need to release the connection, not the delegate. The NSURLConnection class I think does not retain the delegate, which is why you get a crash when you try and release it.
The two places to release the connection are connection:DidFinishLoading, and connection:didFailWithError.