NSURLConnection shown as a leak in devices

Hi, another silly question regarding leaks, as well as NSURLConnection. How can I let him go? Is it enough if I release the following two methods?

(void) connection: (NSURLConnection *) connection didFailWithError: (NSError *) error
(void) connectionDidFinishLoading: (NSURLConnection *) connection

Because in tools, he shows me a line in which I highlight my connection as a leak source.

(EDIT1: OK, I don’t understand it. After the following code, my urlConnection has a save account of 2. WTF?)

NSURLConnection * urlConnection = [[NSURLConnection alloc] initWithRequest: urlRequest delegate: self];

This is the line that the tools point me to.

EDIT2: here is the code:

I create a 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 will let him go 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: , ! !

+3
1

, , , clang, , . , , , , , .

+3

Source: https://habr.com/ru/post/1750627/


All Articles