Re: How to keep things alive in Arc?
Re: How to keep things alive in Arc?
- Subject: Re: How to keep things alive in Arc?
- From: Roland King <email@hidden>
- Date: Mon, 09 May 2016 20:01:42 +0800
> On 9 May 2016, at 19:17, Dave <email@hidden> wrote:
>
> Hi,
>
> Well if “thing” is used only within the method you don’t have to do anything - ARC will keep it alive until the local you have assigned it to goes out of scope.
No that’s not true, which is the whole point of the original question. ARC *may* keep it alive until the local goes out of scope, but it is also free to determine when the variable is last used and free at at any point after that, it doesn’t have to wait until the end of the scope. And in release mode, it fairly often doesn’t.
Which is why you do have to do something. And the two options are the ‘precise lifetime’ which tells ARC explicitly to keep the variable alive through the entire scope whether it thinks it’s used or not, or the ‘returns inner pointer’ which tells ARC that the result of an earlier call returns something which requires the original receiver object to stay alive whilst it’s being used. In that case ARC is free to remove both objects after it determines the inner pointer one is no-longer used (which again may be before either of them go out of scope).
The usual example of the latter is [ NSData bytes ], you need the NSData object to stay alive whilst its bytes are being used, so [ NSData bytes ] is annotated to return an inner pointer.
This is all covered in the clang discussion/documentation which highlights the rationale for allowing objects to be destroyed when it determines they are no-longer used, but before the end of the scope, and the keywords which suppress that behaviour in cases like this.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden