OGeek|极客世界-中国程序员成长平台

标题: ios - 为 NSURLConnection 识别出内存泄漏 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:24
标题: ios - 为 NSURLConnection 识别出内存泄漏

当我运行仪器时,我在下一行得到了内存链接

NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest 
    returningResponse:&urlResponse error:&error];

谁能解释一下如何解决这个问题,

谢谢 山姆。

- (NSString *)sendHttpsReqNSString *) 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];
}



Best Answer-推荐答案


你实际上是在启动 两个 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4