何时在NSURLConnection委托上调用release? [英] When to call release on NSURLConnection delegate?

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

问题描述

将委托人传递给一个 NSUrlConnection 对象,如下所示:

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

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

应该在 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 / release-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/Refer ence.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.

第一个也是最常用的使用与代理初始化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,在连接完成加载后,de减少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 可以在创建连接后立即释放您的处理程序将被连接保留)

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.

编辑:通过执行上述任何选项,您不必担心关于在连接中释放代理(但是您仍然必须释放连接):DidFinishLoading connection: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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆