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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:26:12+00:00 2026-05-11T16:26:12+00:00

Apple is doing this in a setter method for an instance variable mainSprocket: –

  • 0

Apple is doing this in a setter method for an instance variable mainSprocket:

– (void)setMainSprocket:(Sprocket *)newSprocket {
    [mainSprocket autorelease];
    mainSprocket = [newSprocket retain];
    return;
}

Why do they send -autorelease and not -release? Would a -release have a bad effect here? Actually it should not (for my understanding), because the -release just says that the current object held by the instance variable mainSprocket is no longer used by that instance variable. For the case that anyone else is still interested in exactly that object, that method could retain it, right? So -release should be fine, I think?

  • 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-11T16:26:12+00:00Added an answer on May 11, 2026 at 4:26 pm

    They’re doing autorelease here in case newSprocket and mainSprocket happen to be the same object. A call to release might inadvertently deallocate the object before it can be retained on the next line, whereas the autorelease won’t be processed until the autorelease pool is drained at the end of the event loop.

    Consider this scenario:

    Sprocket *mySprocket = [Sprocket spacelySprocket];
    [sprocketManager setMainSprocket:mySprocket];
    
    Sprocket *anotherPointerToMySprocket = mySprocket;
    [sprocketManager setMainSprocket:anotherPointerToMySprocket];
    

    The last line would cause an issue if mainSprocket wasn’t autoreleased. Another convention you might sometimes see for setter code that does the same thing is:

    – (void)setMainSprocket:(Sprocket *)newSprocket {
        if (newSprocket != mainSprocket) {
            [mainSprocket release];
            mainSprocket = [newSprocket retain];
        }
    }
    

    I’ll leave it up to others to comment on which is more appropriate or aesthetically pleasing 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 170k
  • Answers 170k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you look in Context.tt at line 35 you'll see… May 12, 2026 at 2:11 pm
  • Editorial Team
    Editorial Team added an answer You could make a HTTP HEAD request, and get a… May 12, 2026 at 2:11 pm
  • Editorial Team
    Editorial Team added an answer That is because self.aArray makes use of the synthesized property.… May 12, 2026 at 2:11 pm

Related Questions

I routinely run into this problem, and I'm not sure how to get past
I recently started working in Java and was introduced to the wild and crazy
Does anyone have any examples or resources where i might find information on scrolling
Resolution: While trying to recreate this bug in a fresh project to submit to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.