当我运行仪器时,我在下面一行中得到了内存链接
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest
returningResponse:&urlResponse error:&error];
有人可以解释一下如何解决这个问题吗
谢谢 萨姆。
- (NSString *)sendHttpsReq:(NSString *) urlString {
// create the request
NSString *endResult = nil;
NSURL *posHostUrl = [NSURL URLWithString:urlString];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:posHostUrl
cachePolicy:NSURLCacheStorageAllowed
timeoutInterval:300.0];
// create the connection with the request
// and start loading the data
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[posHostUrl host]];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData that will hold
// the received data
// receivedData is declared as a method instance elsewhere
NSHTTPURLResponse* urlResponse = nil;
//NSError *error = [[NSError alloc] init];
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error];
endResult = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//[error release];
} else{
//Inform the user that the connection failed.
NSLog(@"CONNECTION FAILED");
}
[theConnection release];
return [endResult autorelease];
}
最佳答案
您实际上正在启动两个 NSURLConnections。一种是异步的,一种是同步的。这可能会导致泄漏。
第一个 URLConnection 在以下行中启动:
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
第二个 URLConnection 在以下行中启动:
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error];
请确保您只下载资源一次。
关于ios - 识别出 NSURLConnection 的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3340972/