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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:37:47+00:00 2026-05-10T13:37:47+00:00

I’m just beginning to have a look at Objective-C and Cocoa with a view

  • 0

I’m just beginning to have a look at Objective-C and Cocoa with a view to playing with the iPhone SDK. I’m reasonably comfortable with C’s malloc and free concept, but Cocoa’s references counting scheme has me rather confused. I’m told it’s very elegant once you understand it, but I’m just not over the hump yet.

How do release, retain and autorelease work and what are the conventions about their use?

(Or failing that, what did you read which helped you get it?)

  • 1 1 Answer
  • 3 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. 2026-05-10T13:37:48+00:00Added an answer on May 10, 2026 at 1:37 pm

    Let’s start with retain and release; autorelease is really just a special case once you understand the basic concepts.

    In Cocoa, each object keeps track of how many times it is being referenced (specifically, the NSObject base class implements this). By calling retain on an object, you are telling it that you want to up its reference count by one. By calling release, you tell the object you are letting go of it, and its reference count is decremented. If, after calling release, the reference count is now zero, then that object’s memory is freed by the system.

    The basic way this differs from malloc and free is that any given object doesn’t need to worry about other parts of the system crashing because you’ve freed memory they were using. Assuming everyone is playing along and retaining/releasing according to the rules, when one piece of code retains and then releases the object, any other piece of code also referencing the object will be unaffected.

    What can sometimes be confusing is knowing the circumstances under which you should call retain and release. My general rule of thumb is that if I want to hang on to an object for some length of time (if it’s a member variable in a class, for instance), then I need to make sure the object’s reference count knows about me. As described above, an object’s reference count is incremented by calling retain. By convention, it is also incremented (set to 1, really) when the object is created with an ‘init’ method. In either of these cases, it is my responsibility to call release on the object when I’m done with it. If I don’t, there will be a memory leak.

    Example of object creation:

    NSString* s = [[NSString alloc] init];  // Ref count is 1 [s retain];                             // Ref count is 2 - silly                                         //   to do this after init [s release];                            // Ref count is back to 1 [s release];                            // Ref count is 0, object is freed 

    Now for autorelease. Autorelease is used as a convenient (and sometimes necessary) way to tell the system to free this object up after a little while. From a plumbing perspective, when autorelease is called, the current thread’s NSAutoreleasePool is alerted of the call. The NSAutoreleasePool now knows that once it gets an opportunity (after the current iteration of the event loop), it can call release on the object. From our perspective as programmers, it takes care of calling release for us, so we don’t have to (and in fact, we shouldn’t).

    What’s important to note is that (again, by convention) all object creation class methods return an autoreleased object. For example, in the following example, the variable ‘s’ has a reference count of 1, but after the event loop completes, it will be destroyed.

    NSString* s = [NSString stringWithString:@'Hello World']; 

    If you want to hang onto that string, you’d need to call retain explicitly, and then explicitly release it when you’re done.

    Consider the following (very contrived) bit of code, and you’ll see a situation where autorelease is required:

    - (NSString*)createHelloWorldString {     NSString* s = [[NSString alloc] initWithString:@'Hello World'];      // Now what?  We want to return s, but we've upped its reference count.     // The caller shouldn't be responsible for releasing it, since we're the     // ones that created it.  If we call release, however, the reference      // count will hit zero and bad memory will be returned to the caller.       // The answer is to call autorelease before returning the string.  By      // explicitly calling autorelease, we pass the responsibility for     // releasing the string on to the thread's NSAutoreleasePool, which will     // happen at some later time.  The consequence is that the returned string      // will still be valid for the caller of this function.     return [s autorelease]; } 

    I realize all of this is a bit confusing – at some point, though, it will click. Here are a few references to get you going:

    • Apple’s introduction to memory management.
    • Cocoa Programming for Mac OS X (4th Edition), by Aaron Hillegas – a very well written book with lots of great examples. It reads like a tutorial.
    • If you’re truly diving in, you could head to Big Nerd Ranch. This is a training facility run by Aaron Hillegas – the author of the book mentioned above. I attended the Intro to Cocoa course there several years ago, and it was a great way to learn.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.