简体   繁体   English

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

[英]How to release the NSUrlConnection class object,NSMutableData class object?

iam making one applciation.In that iam using the NSUrlconnection class.Below one is my code. iam进行一个应用程序。在该iam中,使用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];
    }

In this code when iam executing,it shows the memory leak at responsedata = [[NSMutableData data] retain]; 在此代码中,当执行iam时,它表示在responsedata = [[NSMutableData data] retain];内存泄漏responsedata = [[NSMutableData data] retain]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; in viewDidLoad() .SO please tell me where it released. viewDidLoad() SO中,请告诉我它的发布位置。

  1. You should save reference to your NSURLConnection : 您应该保存对NSURLConnection引用:

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

  2. You should start it: 您应该启动它:

    [connection start];

  3. And you should release it in didFailWithError or connectionDidFinishLoading . 并且您应该在didFailWithErrorconnectionDidFinishLoading释放它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM