views:

147

answers:

2

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!

+2  A: 

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.

Graham Lee
Thanks. i also found it strange that a totally empty iPhone project "View based project" with a few controls such as textfield, buttons, switches added is reported by instruments that it leaks memory. At least there should be a checkbox titled "my leaks only", or something similar, i care about my leaks only. It HAS reported some true memory leaks which i corrected but there were some very suspicios ones, like this one
kudor gyozo
@Gyozo: right. Leaks is designed in such a way that it will generate false +ves more often than you'd like, but hopefully doesn't have too many false -ves. It's also possible that UIKit genuinely does have some leaks, but the tool is not 100% accurate.
Graham Lee
A: 

Problem solved by not caring anymore whether it's leaking or not! Simple!

kudor gyozo