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.
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 initWithRequest:delegate: Special Considerations: The connection retains delegate. It releases delegate when the connection finishes loading, fails, or is canceled.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. NSURLConnection alloc initWithRequest:request self; 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. //creates the handler object MyHandlerClass *handler = MyHandlerClass alloc init; //creates the connection with handler as an autorelease object NSURLConnection alloc initWithRequest:request delegate:handler autorelease; OR you could release your handler right after creating the connection (as it will be already retained by the connection) //creates the handler object MyHandlerClass *handler = MyHandlerClass alloc init; //creates the connection with handler NSURLConnection alloc initWithRequest:request delegate:handler; //releases handler object handler release; 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 and connection:didFailWithError methods.
It will depend on what object handler is and how you use it. For example, I usually use self as my delegate: NSURLConnection alloc initWithRequest:request delegate:self; I don't need to call release on self because delegates are not retained and self 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 the handler 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.
Yeah, that's what I thought too, but when I do that... I get the exec bad access gods smiting me. – Kieran H Oct 28 '09 at 9:03 2 Actually, I'm pretty sure NSURLConnection does retain the delegate (it's an exception in this regard). – shosti Mar 22 '10 at 15:24 1 NSURLConnection does indeed retain the delegate, check the documentation.
– Julio Gorgé Jun 10 '10 at 2:21.
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: @interface DataService : NSObject {} - (void) search:(NSString *) name byAddress:(NSString *)address; @end so the implementation of that method creates the delegate to pass: SearchDelegate *delegate = SearchDelegate alloc init; self sendRequestToUrl:urlString withJson:jsonString andHandler:delegate; 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: // http code setup blah... NSURLConnection alloc initWithRequest:request delegate:handler; handler release; and this seems to have gotten ridden of memory leak being reported in Instruments.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.