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); });
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:
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.self
, because self
is no longer involved in retrieving the address of the object in questionIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With