pseudopeach
pseudopeach

Reputation: 1475

Help with a memory leak

I made a class that uses NSURLConnection and KVC to make objects from plists on a server. Instruments says I have a bunch of memory leaks coming from the function that handles the data returned from the server:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    hasOutstandingCall = NO;
    NSError* sError;

    NSDictionary* tmpDict = [NSPropertyListSerialization propertyListWithData:receivedData 
        options:NSPropertyListImmutable
        format:NULL error:&sError];
    self.lastResponse = [ServerResponse new];
    //NSLog(@"server responded: %@",tmpDict);
    [lastResponse setValuesForKeysWithDictionary:tmpDict];
    if(tmpDict.count == 0){
        lastResponse.success = NO;
        lastResponse.errorId = -1;
        lastResponse.errorMessage = @"Couldn't understand server response";
        [[NSNotificationCenter defaultCenter] postNotificationName:@"serverDidRespond" object:self];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"requestDidFail" object:self];
        NSLog(@"failed to deserialize response");
        NSString* serverMessage = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
        NSLog(@"SERVER SAID: %@",serverMessage);

        [serverMessage release];
        [receivedData release];
        return;
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:@"serverDidRespond" object:self];

    if(lastResponse.success){
        if(lastResponse.resultQuery)
            NSLog(@"%@ response: query with %d rows",lastFName, lastResponse.resultQuery.count);
        else if(lastResponse.resultObject)
            NSLog(@"%@ response: object",lastFName);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"requestDidSucceed" object:self];
    }else{
        NSLog(@"%@ response: ERROR id: %d, message: %@",lastFName, lastResponse.errorId, lastResponse.errorMessage);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"requestDidFail" object:self];
    }

    [receivedData release];
}

Instruments says I am leaking a server response, and a bunch of other things that pass through this function. Does the "Responsable Frame" thing always refer to whatever originally created the leaked object no matter what? Should I just be looking for how this stuff gets leaked down the road or do I have a problem here in this function? From what I understand, tempDict, sError are autoreleased when they come back from serialization. I'm sending the supposedly leaked serverResponse into a synthesized setter that gets released in the dealloc method, so I don't see what the problem is. Anyone got some insight?

Upvotes: 0

Views: 477

Answers (1)

bbum
bbum

Reputation: 162712

self.lastResponse = [ServerResponse new];

That is most likely a double retain, assuming that lastResponse is an @property declared as retain (or your setter retains).

When Instruments identifies a leak, it shows where the item was allocated, but that may not be the cause of the leak. The leak will always be an unbalanced retain, but only sometimes will that retain be on the same line of the allocation (as it was in this case, apparently).

Upvotes: 2

Related Questions