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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:16:56+00:00 2026-05-23T08:16:56+00:00

In the example below, I don’t understand why localVariable is being accessed by value

  • 0

In the example below, I don’t understand why localVariable is being accessed by value in doSomethingWithObject. What makes that conversion? How do you distinguish between accessing a variable by value and accessing it by reference? I would like to see more related examples if possible.

The following is an excerpt from Apple’s Blocks Programming Topics and shows how instance variables are retained in blocks.

If you use a block within the
implementation of a method, the rules
for memory management of object
instance variables are more subtle:

  • If you access an instance variable by reference, self is retained;
  • If you access an instance variable by value, the variable is retained.

The following examples illustrate the
two different situations:

dispatch_async(queue, ^{
    // instanceVariable is used by reference, self is retained
    doSomethingWithObject(instanceVariable);
});

id localVariable = instanceVariable;
dispatch_async(queue, ^{
    // localVariable is used by value, localVariable is retained (not self)
    doSomethingWithObject(localVariable);
});
  • 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-23T08:16:57+00:00Added an answer on May 23, 2026 at 8:16 am

    It’s because when you access an instance variable directly, the compiler (more or less) translates that into a struct member lookup. So:

    [ivar doSomething];
    

    Becomes

    [self->ivar doSomething];
    

    Because self is needed, self must be retained. However, when you copy the pointer value into a new variable, you know longer need the self struct to know what the pointer value is, and thus self does not need to be retained, because the pointer value can be const copied off the stack. That cannot happen with an instance variable (because the ivar could change between when the block is created and when the block is executed).


    Clarification:

    • a block must retain all of the objects that it references internally to ensure that those objects will continue to exist for the lifetime of the block.
    • when you access an ivar directly in code, you’re really just looking up a member of a struct (since Objective-C objects are really just structs)
    • looking up a member of a struct means that you have to have the struct
    • thus, a block will retain the struct (in this case, self) so that the lookup will always succeed. If it did not do this, then self could potentially be deallocated in the future, and now the struct lookup would cause a bad access (most likely) and your app would crash.
    • alternatively, you could create a new object pointer locally in the current stack frame. The upside of this is that you no longer have to retain self, because self is no longer involved in retrieving the address of the object in question
    • of course, the object referenced by the local variable will be retained to ensure that it exists for the lifetime of the block.
    • using the terms “by value” and “by reference” here is completely incorrect. Objects in Objective-C are always passed by reference, since we always are passing pointers. You cannot pass an object by-value in Objective-C. (there are some caveats to this, but you really don’t want to go there) For more information on what it means to pass something by-value versus by-reference, check out this question: What's the difference between passing by reference vs. passing by value?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The example below, is just an example, I know that I don't need an
I need help explaing the following example below (c) CommonsWare. I know it makes
I have a real-life example that I have in my project below. My aim
I have the array example below that I am using to dynamically create an
The example below throws an InvalidOperationException, Collection was modified; enumeration operation may not execute.
In my example below I'm using a dijit.form.DateTextBox : <input type=text name=startDate dojoType=dijit.form.DateTextBox constraints={datePattern:'MM/dd/yyyy'}
In the example below I have a ListBox with dozens of font names in
I usually do something like the example below when I need to return error
How can I select the good method (I have in the example below show
Working with a latex document with eps images as in the example below... \documentclass[11pt]{paper}

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.