Why does ARC retain method arguments? Why does ARC retain method arguments? objective-c objective-c

Why does ARC retain method arguments?


See this reply from the Objc-language mailing list:

When the compiler doesn't know anything about the memory management behavior of a function or method (and this happens a lot), then the compiler must assume:

1) That the function or method might completely rearrange or replace the entire object graph of the application (it probably won't, but it could). 2) That the caller might be manual reference counted code, and therefore the lifetime of passed in parameters is not realistically knowable.

Given #1 and #2; and given that ARC must never allow an object to be prematurely deallocated, then these two assumptions force the compiler to retain passed in objects more often than not.

I think that the main problem is that your method’s body might lead to the arguments being released, so that ARC has to act defensively and retain them:

- (void) processItems{    [self setItems:[NSArray arrayWithObject:[NSNumber numberWithInt:0]]];    [self doSomethingSillyWith:[items lastObject]];}- (void) doSomethingSillyWith: (id) foo{    [self setItems:nil];    NSLog(@"%@", foo); // if ARC did not retain foo, you could be in trouble}

That might also be the reason that you don’t see the extra retain when there’s just a single call in your method.


Passing as a parameter does not, in general, increase the retain count. However, if you're passing it to something like NSThread, it is specifically documented that it will retain the parameter for the new thread.

So without an example of how you're intending to start this new thread, I can't give a definitive answer. In general, though, you should be fine.


Even the answer of soul is correct, it is a bit deeper than it should be:

It is retained, because the passed reference is assigned to a strong variable, the parameter variable. This and only this is the reason for the retain/release pair. (Set the parameter var to __weak and what happens?)

One could optimize it away? It would be like optimizing every retain/release pairs on local variables away, because parameters are local variables. This can be done, if the compiler understands the hole code inside the method including all messages sent and functions calls. This can be applied that rarely that clang even does not try to do it. (Imagine that the arg points to a person (only) belonging to a group and the group is dealloc'd: the person would be dealloc'd, too.)

And yes, not to retain args in MRC was a kind of dangerous, but typically developers know their code that good, that they optimized the retain/release away without thinking about it.