views:

119

answers:

2

Look at the following method:

-(void)updateProfile:(Profile *)profile WithJSON:(NSString *)JSON;
{
    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *object = [parser objectWithString:JSON error:nil];

    NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
    [nf setPositiveFormat:@"#,##0"];

    profile.displayName = [object valueForKey:@"displayName"];
    profile.profileURL = [object valueForKey:@"profileURL"];

    NSString *rep = [object valueForKey:@"reputation"];
    profile.reputation = [[nf numberFromString:rep] intValue];
    //[rep release];   <-Why not release?

    [nf release];        
    //[object release];  <-Why not release?
    [parser release];
}

I have commented out two lines, which gives me EXC_BAD_ACCESS if not.
Can someone explain to me why it's wrong to release these objects?

+11  A: 

You shouldn't release it because you didn't +alloc, -retain, or -copy it. Convenience constructors like +objectWith… return autoreleased objects.

NSResponder
Thanks! Wow. That was kind of obvious... And it solved some problems I thought came from RegexKitLite-framework, too.One day, I will get this. I hope...
Vegar
+5  A: 

The better question to ask is: Why should you release it? What have you done to claim ownership over the object? The answer in this case is "nothing." Since you don't own it, you can't very well release it.

Chuck
Guess I have to stop destroying other mans property... Thanks.
Vegar