【问题标题】:Memory leak identified for NSURLConnection为 NSURLConnection 识别出内存泄漏
【发布时间】:2011-03-21 10:29:25
【问题描述】:

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

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];
}

【问题讨论】:

    标签: ios iphone objective-c memory-leaks


    【解决方案1】:

    你实际上是在启动 两个 NSURLConnections。一个异步,一个同步。这可能会导致泄漏。

    第一个 URLConnection 在该行开始:

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    

    第二个 URLConnection 在该行开始:

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

    请确保您只下载一次资源。

    【讨论】:

      猜你喜欢
      • 2014-08-13
      • 2013-03-19
      • 2023-04-08
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      相关资源
      最近更新 更多