3

你好,另一个关于泄漏和NSURLConnection的愚蠢问题。我如何释放它?如果我在以下两种方法中释放,是否够了?显示为仪器泄漏的NSURLConnection

 
(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
(void)connectionDidFinishLoading:(NSURLConnection *)connection 

因为在乐器中它向我显示了我将连接分配为泄漏源的线。

 
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest: urlRequest delegate: self]; 

这是仪器指向我行(EDIT1 OK我不明白我的URLConnection具有下面的代码后保留2 WTF的计数?)。

EDIT2:这里是一些代码:

我在这里创建了连接

 
- (void) makeRequest 
{ 

    //NSString *urlEncodedAddress = [self.company.street stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; 

    NSString *urlString = [[NSString alloc] initWithFormat: 
           @"http://maps.google.com/maps/api/geocode/xml?latlng=%f,%f&sensor=false", 
           bestEffort.coordinate.latitude,bestEffort.coordinate.longitude]; 
    debugLog(@"%@",urlString); 

    NSURL *url = [[NSURL alloc] initWithString: urlString]; 
    [urlString release]; 
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL: url]; 
    [url release]; 
    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest: urlRequest delegate: self]; 
    debugLog(@"connection created %@ rc %i", urlConnection, urlConnection.retainCount); 
    [urlRequest release]; 
    connection = urlConnection; 
} 

我释放在这里

 
-(void)connection:(NSURLConnection *)_connection didFailWithError:(NSError *)error 
{ 
    debugLog(@"ERROR with the connection: %@", error.localizedDescription); 
    //[activityIndicator setHidden:YES]; 

    debugLog(@"connection will be released or else %@ %i", _connection, [_connection retainCount]); 

    [connection release]; 
    connection = nil; 
    [webData release]; 
    webData = nil; 

    if (!cancel) 
     [delegate rgc_failedWithError: self : error]; 

    isWorking = FALSE; 

} 

或者这里

 
-(void)connectionDidFinishLoading:(NSURLConnection *)_connection 
{ 
    debugLog(@"connection will be released (or else) %@ %i", _connection, [_connection retainCount]); 

    [connection release]; 
    connection = nil; 

    debugLog(@"DONE. Received Bytes: %d", [webData length]); 
    //NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
    //debugLog(@"%@",theXML); 
    //[theXML release]; 
     ..... 
     ..... 
} 

EDIT3:问题解决不关心无论是否泄漏!简单!

+1

如果你发布了一些源代码,将会更容易给你一个正确的答案 – 2010-06-18 07:58:00

+0

嗯,我建立一个连接,马上就有rc:2(!)。在发布错误或成功事件处理程序之前,它仍然有2个rc。 – 2010-06-18 08:30:50

+0

顺便说一句,如果我做一个额外的释放刚刚alloc后,它给了我一个EXC_BAD_ACCESS在connectionDidFinishLoading :) – 2010-06-18 08:45:20

回答

3

在代表方法中发布它是正确的,但是像仪器和铛分析器这样的分析工具不够聪明,无法处理并报告误报。我倾向于向苹果提交一个bug,它肯定会重复,但会告诉他们更多的开发人员正在发现这个烦人的事情。

+0

谢谢。我还发现奇怪的是,一个完全空的iPhone项目“基于视图的项目”与一些控件,如文本框,按钮,开关添加是由仪器报告泄漏内存。至少应该有一个标题为“仅我的泄漏”的复选框,或类似的东西,我只关心我的泄漏。它已经报告了一些真正的内存泄漏,我纠正了,但有一些非常可疑的,像这样一个 – 2010-06-18 08:29:04

+0

@Gyozo:对。泄漏的设计方式使得它会比你想要的更频繁地产生错误信息,但希望没有太多错误信息。 UIKit也有可能确实存在一些泄漏,但该工具并非100%准确。 – 2010-06-18 10:29:45