Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding memory management in Objective-C
    primarykey
    data
    text
    <p>I've got the following code that works OK, but I'm not sure if I understood correctly some memory management concepts:</p> <pre><code>#import "mapPoint.h" @implementation mapPoint @synthesize coordinate, title, subtitle; -(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t { [super init]; coordinate = c; [self setTitle:t]; // Set date as subtitle NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; NSString *myDate = [dateFormatter stringFromDate:[NSDate date]]; [self setSubtitle:myDate]; [dateFormatter release]; // Look for city and state; when found, set it in subtitle, replacing date geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:c]; [geocoder setDelegate:self]; [geocoder start]; return self; } -(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error { NSLog(@"%@", error); } -(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark { [self setSubtitle:[NSString stringWithFormat:@"City: %@, State: %@", [placemark locality], [placemark administrativeArea]]]; } -(void)dealloc { [title release]; [subtitle release]; [geocoder release]; [super dealloc]; } @end </code></pre> <ol> <li>As I created the geocoder object via the <code>alloc</code> method, I have to release it (done in <code>dealloc</code>). Correct?</li> <li>In method <code>reverseGeocoder:didFindPlacemark</code> a NSString is created with the convenience method <code>stringWithFormat</code>. As I didn't use <code>alloc</code>, I'm not responsible for releasing that (I assume that method uses <code>autorelease</code>). Is that right?</li> <li>The object <code>placemark</code> returns two NSStrings, <code>locality</code> and <code>administrativeArea</code>. Again, I didn't create those strings, so I'm not releasing them. However, given that they are part of the subtitle property of my mapPoint object, I don't want those strings to disappear, but they probably were created with <code>autorelease</code>. The property <code>subtitle</code> is declared with <code>retain</code>. Is is correct to assume that it will retain the NSString created with the convenience method, preventing it's premature destruction?</li> </ol> <p>Thanks, and apologies if the questions are complicated... the subject is.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload