Dell P
Dell P

Reputation: 31

Proper way to release memory allocation?

I'm a newbie when it comes to objective C, and am currently experiencing a memory leak with the following code snippet. The memory leak occurs with the 'responseObj' allocation. Whenever I try to release it similar to responseData, I get a crash.

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString* responseStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSMutableArray* responseObj = [responseStr objectFromJSONString];
    [delegate loadGameDetails:[responseObj objectForKey:@"result"]];
    [responseStr release];

    [responseData release]; responseData = nil; 
}

I also tried to autorelease as below, but I also get a crash:

[delegate loadGameDetails:[[responseObj objectForKey:@"result"] autorelease]];

What's the proper way for me to release this block of memory?

Upvotes: 3

Views: 174

Answers (2)

jrturton
jrturton

Reputation: 119242

Nothing in that code is leaking. You have a possible over-release of responseData but I can't tell since the declaration and setup of it is not in scope. If it is an ivar you would typically set it to nil via the accessor (which would release it there) than directly as you have done here.

If there is a leak it is in your loadGameDetails method. What makes you think you have a memory leak? Have you run this through instruments, or used the static analyser?

Upvotes: 1

gschandler
gschandler

Reputation: 3208

The problem is not in your allocation. Your are sending objectForKey: to a NSMutableArray which is actually a NSDictionary/NSMutableDictionary method. Double check what type of object objectFromJSONString really returns.

Upvotes: 2

Related Questions