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

  • SEARCH
  • Home
  • 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 3724222
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:33:43+00:00 2026-05-19T06:33:43+00:00

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

  • 0

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

item is tied to a ManagedObjectContext. The managed variables in item such as place have setters/getters created with @dynamic. Thus, the declaration is

@property (nonatomic, retain) NSString *place;
@dynamic place;

Later in the code, in the ReverseGeocoderDelegate, I access it:

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {

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?

  • 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-19T06:33:44+00:00Added an answer on May 19, 2026 at 6:33 am

    First of all, I would change the logic like this:

    NSString *newPlace = nil;
    
    if (placemark.thoroughfare && placemark.subThoroughfare) {
        newPlace = [NSString stringWithFormat:@"%@ %@", placemark.thoroughfare , placemark.subThoroughfare];
    }
    else {
        if (placemark.thoroughfare) {
            newPlace = [NSString stringWithFormat:@"%@ ", placemark.thoroughfare];
        }
        else {
            newPlace = @"Unknown Place";
        }
    }
    
    item.place = newPlace;    // Either nil of valid string can be assigned to
    

    Using release for simply re-initializing pointer is not so recommended by many. You don’t have to do this if it was to save some memory.

    Your previous logic does releasing twice. What release does initially is simply decrementing retainCount. If it’s 0, then the object is deallocated. Object will not be deallocated until its retainCount is 0.

    Assuming your item has the property retain and since stringWithFormat: returns autoreleased string, so at the 2nd release, you were trying to release what was going to be autoreleased anyway.

    Best way to clearing an object multiple times is simply assigning nil to it.

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

Sidebar

Related Questions

I have some HTML that looks like this: <ul class=toggleList> <li><input type=checkbox name=toggleCbx1 id=toggleCbx1
I want to have a hierarchy that looks like this (and it has to
I have my shared user defaults plist that looks something like: menuItems (Array) Item
I have a table called 'MyTable' that looks like: ID | Item | Type
Looks like _compile_select is deprecated and get_compiled_select is not added to 2.1.0. Are there
Looks like this question is popular, but there is no jQuery version. I have
Looks like I didn't set up my actor correctly, but not sure how else
Looks like XSD.exe is not delivered as a part of Visual Studio 2010. what
Looks like dynamic memory allocation without garbage collection is a way to disaster. Dangling
Looks like operator new and operator new[] have exactly the same signature: void* operator

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.