Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4021736
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:23:20+00:00 2026-05-20T10:23:20+00:00

I’ve got the following code that works OK, but I’m not sure if I

  • 0

I’ve got the following code that works OK, but I’m not sure if I understood correctly some memory management concepts:

#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 
  1. As I created the geocoder object via the alloc method, I have to release it (done in dealloc). Correct?
  2. In method reverseGeocoder:didFindPlacemark a NSString is created with the convenience method stringWithFormat. As I didn’t use alloc, I’m not responsible for releasing that (I assume that method uses autorelease). Is that right?
  3. The object placemark returns two NSStrings, locality and administrativeArea. 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 autorelease. The property subtitle is declared with retain. Is is correct to assume that it will retain the NSString created with the convenience method, preventing it’s premature destruction?

Thanks, and apologies if the questions are complicated… the subject is.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:23:20+00:00Added an answer on May 20, 2026 at 10:23 am
    1. geocoder is an ivar in your class? Then your code is correct. Note that, since the geocoder is useless after it returns a result, you could release it (and set the ivar to nil) in the appropriate delegate methods to allow the memory to be reclaimed earlier.
    2. Correct, mostly. If you had used a method starting with “new” or containing “copy” to get the instance of the object, you would also be responsible for releasing it.
    3. Correct on all points. Note that if you were to implement setSubtitle: yourself instead of allowing @synthesize to do it, you would be responsible for implementing that behavior in your implementation of setSubtitle:.

    Edit:

    I see in comments that you are particularly concerned about point 3. The strings from [placemark locality] and [placemark administrativeArea] are passed as parameters to NSString’s stringWithFormat method, after which they are irrelevant because their content has been copied into the new string that stringWithFormat returns. That is the only string you really have to worry about, and as has been pointed out setSubtitle: will retain that string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.