我使用NSURLConnection
类来获得在互联网上的一些数据,我的应用程序得到在某个时刻崩溃,我得到一个错误表明我已经双重释放的对象,Shoud我释放NSURLConnection实例变量?
以下是我创建的一个实例NSURLConnection
我如何释放它:
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[connection release];
我应该释放它时,即时通讯使用它做,对吗?或者它在幕后发布?
编辑
现在,我得到这个问题整理出来,只是我做的NSURLConnection
实例为伊娃和它的dealloc释放,这里的代码
头文件:
// ....
NSURLConnection *connection;
@property (nonatomic, retain) NSURLConnection *connection;
执行文件:
// ...
NSURLRequest *request = [NSURLRequest requestWithURL:url];
self.connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
// ...
-(void)dealloc{
[connection release];
[super dealloc];
}
问题是我没有在分配新连接之前释放旧连接,这是通过在伊娃之前添加自己来完成的。
考虑在您的项目中使用ARC,它将有助于这样的情况。 – 2012-02-04 22:46:34
我总是用它来释放它里面connectionDidFinish加载或connectionDidLoadWithError(不知道确切的名称,但你必须有一个想法)...因为这两种方法都有你正在使用的当前连接的参数... – doNotCheckMyBlog 2012-02-04 23:50:51
你的意思是没有必要如果我设法在didFinishLoading和didFinishWithErro中释放它,可以在dealloc方法中释放它?谢谢 – 4mahmoud 2012-02-05 14:19:17