5

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);
});
Pablo
  • 28,133
  • 34
  • 125
  • 215

1 Answers1

12

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?
Community
  • 1
  • 1
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Not sure I perfectly understand this... but thanks for explanation anyway. – Pablo Jun 22 '11 at 01:25
  • @Dave, not mine. However, I can't connect last sentence with the rest of your explanation. "That" implies to what? Also can't get the const copy part. From what I can simply understand is that if you can substitute ivar with self->ivar, then self is retained, if can not, then ivar is retained. But what is `by value`, `by ref` still not clear to me. Sorry. – Pablo Jun 22 '11 at 05:09
  • 1
    @Dave: I just took the terms "by value" and "by reference" from apple docs and probably that is when all confusion is coming. Let me go through your items and I'll get back. Thx – Pablo Jun 22 '11 at 05:40
  • @Dave: your clarification make sense. But do you agree that in apple docs that I quoted, firstly the statement and example are inconsistent. Second, the statements itself without checking example, leads me to think that if I `access` the ivar by ref then `self` is retained and if I `access` the ivar by val the ivar itself will be retained and not some local variable! The key part is that it says `accessing` and not `passing`. I am aware of the passing since coming from C++. If you also agree that apple doc on this is confusing and mismatching with example, then the answer is fully acceptable. – Pablo Jun 23 '11 at 03:03
  • @Michael In my opinion, the docs are using the incorrect terminology. I mean, I can understand where they're coming from, but it sounds wrong unless you really stop to think about it. – Dave DeLong Jun 23 '11 at 03:34