为NSURLConnection确定内存泄漏

时间:2010-07-27 05:26:49

标签: ios iphone objective-c memory-leaks

当我运行仪器时,我在下面的行中获得了内存链接

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

1 个答案:

答案 0 :(得分:4)

您实际上正在开始两个 NSURLConnections。一个异步和一个同步。这可能导致泄漏。

首先在行中开始URLConnection:

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

第二个URLConnection在行中开始:

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

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

相关问题