When to call release on NSURLConnection delegate?

2019-02-14 02:22发布

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

5条回答
闹够了就滚
2楼-- · 2019-02-14 02:26

NSURLConnection delegate is retained.

Use the code in ViewDidLoad with non-ARC environment.

NSLog(@"Retain count %d",[self retainCount]);
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:nil delegate:self];
NSLog(@"Retain count %d",[self retainCount]);
查看更多
Root(大扎)
3楼-- · 2019-02-14 02:29

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.

查看更多
霸刀☆藐视天下
4楼-- · 2019-02-14 02:38

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

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.

查看更多
相关推荐>>
5楼-- · 2019-02-14 02:46

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?

查看更多
再贱就再见
6楼-- · 2019-02-14 02:48

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.

查看更多
登录 后发表回答