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,
selfis 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); });
It’s because when you access an instance variable directly, the compiler (more or less) translates that into a struct member lookup. So:
Becomes
Because
selfis needed,selfmust be retained. However, when you copy the pointer value into a new variable, you know longer need theselfstruct to know what the pointer value is, and thusselfdoes not need to be retained, because the pointer value can beconstcopied 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:
self) so that the lookup will always succeed. If it did not do this, thenselfcould potentially be deallocated in the future, and now the struct lookup would cause a bad access (most likely) and your app would crash.self, becauseselfis no longer involved in retrieving the address of the object in question