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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:41:52+00:00 2026-05-22T20:41:52+00:00

being a Cocoa/Obj-C newbie I am going through the Cocoa Programming for Mac OS

  • 0

being a Cocoa/Obj-C newbie I am going through the “Cocoa Programming for Mac OS X” book by Aaron Hillegass and – leaving apart the fact that now we have also the chance to use GC to avoid all this reasoning – I am not sure I get the reason for some of those retains.

In particular in one of the examples Aaron gives as good programming practice:

- (void) setFoo:(NSCalendarDate *)x
{
    [x retain];
    [foo release];
    foo = x;
}

I don’t get the reason for retaining the x instance at the first line of the method:

[x retain];

The scope of this instance is just the set method, right?
When exiting the method scope the x instance should be deallocated anyway no?
Besides, when assigning x to foo with:

foo = x;

foo will be anyway pointing to x memory cells and will therefore increment the pointed object retain count, no? This should ensure the memory won’t be deallocated.

So, what’s the point? I am sure I am missing something, of course, but don’t know what exactly.

Thanks,
Fabrizio

  • 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-22T20:41:52+00:00Added an answer on May 22, 2026 at 8:41 pm

    Retain means: I will be needing this object to stay around, it must not be deallocated. If x wouldn’t be retained, the following is likely to happen:

    You assign x to foo, so foo now points to the address where your NSCalendarDate is. Someone releases or autoreleases this object, it’s retain count eventually drops to 0 and the object is deallocated. Now your foo still points to that address, but there’s no longer a valid object. Sometime later, a new object is created and by chance it’s situated at the same address than your old NSCalendarDate object. Now your foo points to an entirely different object !

    To prevent that, you need to retain it. You need to say, please do not deallocate the object yet, I need it. Once you’re done with it, you release it which means I no longer need the object, you can clean it up now if nobody else needs it.

    Now for the classical three part assignment. Consider your setFoo: would look like this:

    - (void) setFoo:(NSCalendarDate *)x
    {
        [foo release];
        [x retain];
        foo = x;
    }
    

    This is a very bad idea. Consider your object is the only one who has retained the NSCalendarDate object, and consider you would then do: [self setFoo:foo];. Might sound silly, but something like this can happen. The flow would now be this:

    1. foo would be released. Its retain count might now drop to 0 and the object will get deallocated.
    2. Whoops, we’re trying to retain and access a deallocated object.

    This is why you always first retain the new object, then release the old object.

    If you’re coming from a Java or .NET background, it is very important to understand that a variable of type Foo * only contains the address of your object, nothing more. In Java or .NET, a variable that points to an object automatically “retains” it, if you will. Not so in Objective-C (in non-GC environments). You could consider a variable of type Foo * to be a weak reference, and you explicitly need to tell Objective-C whether you will still need that object at that address or not.

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

Sidebar

Related Questions

I'm new to Cocoa, and working my way through Hillegass's book. I'm having trouble
I am working along with Cocoa Programming For Mac OS X (a great book).
I'm starting to learn openGL on the mac, and being a cocoa developer already
I have a Cocoa App that depends on the main window being a certain
I am working my way through Learn Cocoa on the Mac which was written
Being new to Cocoa, and probably not knowing all of the potential classes available
I'm tryin to grasp the fundamentals of Cocoa (Iphone) MVC design, but it's being
Being stuck with a legacy database schema that no longer reflects your data model
I'm working on a Cocoa app that monitors what you're listening to in iTunes,
I'm looking at building a Cocoa application on the Mac with a back-end daemon

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.