何时在 NSURLConnection 委托上调用 release?

When to call release on NSURLConnection delegate?(何时在 NSURLConnection 委托上调用 release?)

本文介绍了何时在 NSURLConnection 委托上调用 release?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将委托传递给 NSUrlConnection 对象时,如下所示:

When passing a delegate to the a NSUrlConnection object like so:

[[NSURLConnection alloc] initWithRequest:request delegate:handler];

你什么时候应该在委托上调用 release?它应该在 connectionDidFinishLoading 中吗?如果是这样,我会不断收到 exec_bad_access.我看到我的代表正在通过仪器泄漏.

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.

谢谢

推荐答案

取自我的博文在这里:http://i.ndigo.com.br/2012/01/releasing-nsurlconnection-and-its-delegate/

您必须特别注意委托对象,因为对于 NSURLConnection,委托有一个特殊考虑:它始终被保留.

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.

因此,考虑到这一点,您有多种选择来确保您的委托将被正确释放,我将尝试解释 2 个简单的选择.

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.

第一个也是最常用的,是使用初始化 NSURLConnection 的同一个类作为委托.

The first, and most commonly used, is to use the same class that initialize the NSURLConnection as the delegate.

[[NSURLConnection alloc] initWithRequest:request self];

通过这样做,您的类的保留计数将在连接开始时增加 1,然后在连接完成加载、失败或被取消后减少 1,从而不会导致内存泄漏.

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.

通过执行上述任何选项,您不必担心在 connection:DidFinishLoadingconnection 中释放委托(但您仍然必须释放连接):didFailWithError 方法.

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.

这篇关于何时在 NSURLConnection 委托上调用 release?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:何时在 NSURLConnection 委托上调用 release?

基础教程推荐