你好,另一个关于泄漏和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