你好,另一個關於泄漏和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:問題解決不關心無論是否泄漏!簡單!
如果你發佈了一些源代碼,將會更容易給你一個正確的答案 – 2010-06-18 07:58:00
嗯,我建立一個連接,馬上就有rc:2(!)。在發佈錯誤或成功事件處理程序之前,它仍然有2個rc。 – 2010-06-18 08:30:50
順便說一句,如果我做一個額外的釋放剛剛alloc後,它給了我一個EXC_BAD_ACCESS在connectionDidFinishLoading :) – 2010-06-18 08:45:20