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 229779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:47:40+00:00 2026-05-11T19:47:40+00:00

Still a little confused about Objective-C memory management. I think my confusion stems from

  • 0

Still a little confused about Objective-C memory management. I think my confusion stems from what exactly the autorelease means.

NSString *theBackendResponse = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
NSDictionary *accountDictionary = [theBackendResponse propertyList];
[viewController setAccountDictionary:accountDictionary];

Now, what should I do with the accountDictionary in the setAccountDictionary method of my view controller? Right now I just set the instance variable “accountDictionary” to whatever is returned. Should I set it to a retained one, and then release the one that’s returned? What should my setter code block look like, given that NSString’s propertyList method is autoreleased?

By the way, if I release theBackendResponse, will I lose the accountDictionary? I assume not…

  • 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-11T19:47:40+00:00Added an answer on May 11, 2026 at 7:47 pm

    Calling [objectInstance autorelease] adds an object to the current NSAutoreleasePool. When that pool receives a drain message, it sends a release to all the objects in the pool. If any of those objects’ retainCount reaches 0, they are deallocated at that point. The purpose of autorelease is to allow you to mark an object to be released “some time in the future”. This is especially useful for things like methods that return a newly allocated object but want to release it so that the caller doesn’t have to take ownership of the returned object. A method might look like this:

    - (id)myMethod {
        id myObj = [[SomeClass alloc] init];
    
        ...
    
        return [myObj autorelease];
    }
    

    The caller of myMethod would then retain the return value if they wanted to take ownership of the returned value or ignore it if not. When the current NSAutoreleasePool is drained, myObj will get a release message. If no other objects own it (i.e. have sent it a retain message), it will get deallocated.

    All of this is explained in the Cocoa Memory Management Programming Guide. Even if you’ve already read it, it’s always worth an other read.

    So, to answer your questions:

    First, you should release theBackendResponse. You will leak memory if you do not. You don’t need to know what accountDictionary does with the string: if it needs to keep a reference it will have retained theBackendResponse. You have an ownership of theBackendResponse because you alloc‘d it, so you must relinquish that ownership (via release or indirectly via autorelease).

    Second, you must retain or copy the argument to setAccountDictionary: if you want to keep a reference to that object or value respectively. The standard setter method looks something like this (assuming you do not need atomic semantics):

    -(void)setAccountDictionary:(NSDictionary*)newDict {
      if(newDict != accountDictionary) {
        id tmp = accountDictionary;
        accountDictionary = [newDict copy]; //Since newDict may be mutable, we make a copy so that accountDictionary isn't mutated behind our back.
        [tmp release];
      }
    }
    

    You must also remember to release accountDictionary in the dealloc method:

    - (void)dealloc {
        [accountDictionary release];
        [super dealloc];
    }
    

    Since you appear to be using NSViewController, I assume you’re on Leopard (OS X 10.5) in which case, you should probably be using @property and the @synthesized getter/setter if possible. To do this, add a

    @property (copy,readwrite) NSDictionary * accountDictionary; 
    

    declaration to the class @interface. And add a @synthesize accountDictionary; directive in the @implementation block for your controller class.

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

Sidebar

Related Questions

Apologies -- I'm a newbie using Ruby on Rails. Still a little confused about
I am a little confused about the management for UINavigationController to ModelView. I am
I'm a little confused about this behavior from the ruby (1.9) interpreter class Foo
I am a little confused about the session management in PHP. A lot of
I've read similar questions here but I'm still a little confused. MyCollection extends ArrayList<MyClass>
Okay I have updated my code a little, but I am still not exactly
I'm a little confused about using release vs. debug versions of a dll for
I just read about what send does in Ruby and I'm still confused when
I'm a little confused about the default behaviour of Equals and GetHashCode in C#.
I'm a little confused about com threading models. I have an inproc server and

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.