Hello another stupid question regarding leaks and also NSURLConnection. How do i release it? Is it enough if i release in the following 2 methods?
(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error (void)connectionDidFinishLoading:(NSURLConnection *)connection
Because in instruments it shows me the line where I alloc my connection as the source of leaking.
(EDIT1: OK I don't get it. After the following code my urlConnection has a retain count of 2. WTF?)
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest: urlRequest delegate: self];
This is the line that instruments points me to.
EDIT2: here is some code:
I create the connection here
- (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;
}
I release it here
-(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;
}
Or here
-(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: Problem solved by not caring whether it's leaking or not! Simple!
You're correct to release it in the delegate methods, however the analysis tools like instruments and the clang analyser aren't clever enough to deal with that and report a false positive. I'd be inclined to file a bug with apple, it will certainly be a duplicate but will tell them that more developers are finding this annoying.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With