[英]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中,请告诉我它的发布位置。
You should save reference to your NSURLConnection
: 您应该保存对
NSURLConnection
引用:
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
You should start it: 您应该启动它:
[connection start];
And you should release it in didFailWithError
or connectionDidFinishLoading
. 并且您应该在
didFailWithError
或connectionDidFinishLoading
释放它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.