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