Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing variables by value or reference

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);
});
like image 236
Pablo Avatar asked Jun 21 '11 23:06

Pablo


1 Answers

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?
like image 99
Dave DeLong Avatar answered Sep 30 '22 07:09

Dave DeLong