3

From my current understanding, the recommended way (with ARC enabled) of 'pass by reference' is like:

-(void)somefunc:(someclass **)byref; 
// and 'someclass **' should be inferred to 'someclass * __autoreleasing *'
// am i right?

//or we could just explicitly define it like 
-(void)somefunc:(someclass * __autoreleasing *)byref;

However, from the answer to this thread, Handling Pointer-to-Pointer Ownership Issues in ARC.

It seems -(void)somefunc:(someclass *__strong *)byref could do the trick as well (in demo2 of above link).

1.-(void)somefunc:(someclass * __autoreleasing *)byref;
2.-(void)somefunc:(someclass *__strong *)byref

For the first one, as documented by Apple it should be implicitly rewritten by compiler like this:

NSError * __strong error;
NSError * __autoreleasing tmp = error;
BOOL OK = [myObject performOperationWithError:&tmp];
error = tmp;

It seems the second one has a better performance? Because it omits the process of 'assign the value back' and 'autoreleasing'. But I rarely see functions declared like this. Is it a better way to use the second function to do the 'pass by reference' job?

Any suggestion or explanation? Thanks in advance.

Community
  • 1
  • 1
Dirk Zhou
  • 51
  • 3

1 Answers1

-1

the second function isnt thread-safe / 'delay safe'. the first one is more correct.

same reason as to why blocks capture params and performSelector retains the object.


imagine the caller fA allocs a strong reference to A and then calls an ASYNC function fB. fA is done, fB not yet called.... so who retains A in the meantime?

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • thanks for your reply. But i didn't understand your second part well(imagine...). Could you please give me a code example/snippet? – Dirk Zhou Mar 28 '13 at 02:01