Memory management in iOS

Posted by angrest on Stack Overflow See other posts from Stack Overflow or by angrest
Published on 2011-01-16T11:25:39Z Indexed on 2011/01/16 11:53 UTC
Read the original article Hit count: 194

Looks like I did not understand memory management in Objective C... sigh.

I have the following code (note that in my case, placemark.thoroughfare and placemark.subThoroughfare are both filled with valid data, thus both if-conditions will be TRUE

if (placemark.thoroughfare) {
    [item.place release];
    item.place = [NSString stringWithFormat:@"%@ ", placemark.thoroughfare];        
} else {
    [item.place release];
    item.place = @"Unknown Place";
}
if (placemark.thoroughfare && placemark.subThoroughfare) {
// *** problem is here ***
    [item.place release];
    item.place = [NSString stringWithFormat:@"%@ %@", placemark.thoroughfare , placemark.subThoroughfare];
}

If I do not release item.place at the marked location in the code, Instruments finds a memory leak there. If I do, the program crashes as soon as I try to access item.place outside the offending method.

Any ideas?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ios