connectionDidFinishLoading中的连接释放方法,导致错误 [英] connection release method in connectionDidFinishLoading, causes error

查看:95
本文介绍了connectionDidFinishLoading中的连接释放方法,导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试请求并在苹果官方开发者网站上获取响应示例.

testing request and getting the response example in official apple developer site.

https://developer.apple. com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

在connectionDidFinishLoading中使用[connection release]方法, 导致错误:应该无效-EXC_BAD_ACCESS

using [connection release] method in connectionDidFinishLoading, causes error :Should've been invalidated - EXC_BAD_ACCESS

如果我注释行[连接释放]方​​法;一切似乎都正常,但是 恐怕由于不存在连接释放而发生内存泄漏.

if I comment line [connection release] method; everything seems working but Im afraid there is memory leak occurs because of non existent release of connection.

可能是什么原因或如何避免此问题?

what may cause or how can I avoid this problem ?

@imlementation test
NSMutableData *dataStore=nil;


//example usage:
//[self registerToServer:@"http://testserver.com/registeruser.ashx" withUserName:@"john doe" withPassword:@"123456"];

-(void)registerToServer:(NSString*)urlstr withUserName:(NSString*)
ausername withPassword:(NSString*)apassword
{
  NSURL *url=[NSURL URLWithString:urlstr];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:3.0f];
[request setHTTPMethod:@"POST"];
[request setValue:ausername forHTTPHeaderField:@"pass"];
[request setValue:apassword forHTTPHeaderField:@"username"];
NSURLConnection *connection=[NSURLConnection connectionWithRequest:request delegate:self];
[connection start];

if(connection)
    dataStore=[[NSMutableData data]retain];
}


-(无效)连接:(NSURLConnection *)连接didReceiveData:(NSData *)数据 { [dataStore appendData:data]; }


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [dataStore appendData:data]; }

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[dataStore setLength:0];}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"connection failed:%@ %@",
      [error localizedDescription],
      [[error userInfo]objectForKey:NSURLErrorFailingURLStringErrorKey]);

[connection release];
[dataStore release];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
  {
    //[connection release];   //WELL... I CANT COMMENT OUT THIS LINE!
    NSString *res=[[NSString alloc]initWithData:dataStore encoding:NSUTF8StringEncoding];

    NSLog(@"%@",res);
[dataStore release];
  }

推荐答案

您使用工厂方法创建连接.仅当使用alloc/initnewcopyretain时,才应使用release.在这种情况下,系统将为您释放对象.

You create your connection using a factory method. Only if you use alloc/init, new, copy, or retain should you use release. In this case, the system will take care of releasing the object for you.

更好的是,使用ARC.

Better yet, use ARC.

这篇关于connectionDidFinishLoading中的连接释放方法,导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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