如何释放NSUrlConnection类对象,NSMutableData类对象?

时间:2011-09-28 09:19:43

标签: iphone nsurlconnection nsurl

我做了一个applciation.In使用NSUrlconnection类。其中一个是我的代码。

- (void)viewDidLoad {
[super viewDidLoad];
responsedata = [[NSMutableData data] retain];
NSString *url = [NSString stringWithFormat:@"https://www.google.com"];
NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request=[[NSURLRequest alloc]initWithURL:URL];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

[request release];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{

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

  - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    }
  - (void)connectionDidFinishLoading:(NSURLConnection *)connection {

     [connection release];
    }

在执行iam的代码中,它显示内存泄漏 responsedata = [[NSMutableData data] retain]; [[NSURLConnection alloc] initWithRequest:request delegate:self];中的viewDidLoad() .SO请告诉我它的发布地点。

1 个答案:

答案 0 :(得分:1)

  1. 您应该保存对NSURLConnection

    的引用

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

  2. 你应该开始它:

    [connection start];

  3. 您应该在didFailWithErrorconnectionDidFinishLoading中发布。