Reputation: 38228
Consider the following program:
#include <objc/nsobject.h>
#include <stdio.h>
void function(NSObject** object) {
printf("value of NSObject** in function: %p\n", object);
}
int main() {
NSObject* object;
printf("value of NSObject** in main: %p\n", &object);
function(&object);
}
Program output (compiled with -fobjc-arc
on x86_64):
value of NSObject** in main: 0x7ffee1217668
value of NSObject** in function: 0x7ffee1217660
Why are the values not equal? They're offset by 8 bytes, which I find unexpected. If I change it to a plain C type (e.g. int**
) then they both print the same values. But double pointers to Objetive-C objects always give different values.
What's going on here, and why does &object
evaluate to two different values?
Upvotes: 1
Views: 49
Reputation: 90611
ARC is creating an autoreleasing temporary object and passing the address of that and then, after function
returns, assigning that to the strong object
variable.
See Ownership Inference: Indirect parameters to see why function
's parameter is inferred to be __autoreleasing
. See Passing to an out parameter by writeback to see why ARC creates the temporary.
I believe you could declare an explicit __strong
qualifier on function
's parameter to avoid that.
Upvotes: 2