I’ve got a method that creates a string then appends additional strings onto it:
-(NSString*)returnDetails {
NSString *details = [[NSString alloc] init];
details = [details stringByAppendingString:url];
details = [details stringByAppendingString:@" : "];
details = [details stringByAppendingString:author];
return [details autorelease];
}
And I’m getting this error:
iphoneapp_1(66508,0xacd9e2c0) malloc: *** error for object 0x6b9eb80: pointer being freed was not allocated
If I change it to
NSString *details = [NSString string];
and remove the autorelease call, then it works. I would just like to understand why this works and my original method didn’t?
The
NSString‘s methodstringByAppendingString:returns an object that is already autoreleased. So returning[details autorelease]from your method will make that object will be released one more time than it must. Just returndetailsinstead.You also have a memory leak in there, because you never released the string you allocated at the top of the method. Try this code :
}