I use goto in places with ARC without too much trouble. When I get this
warning, it is because ARC cannot figure out the lifetime of an object created
after a possible branch and before the the . Sometimes you can get rid of the
error by simply making the lifetime clear. Here's a silly example that causes
the error:
- (BOOL)test:(BOOL)error
{
if (error) goto cleanup;
NSString* theString = [[NSString alloc] init];
//do stuff with theString;
cleanup:
return error;
}
Here's how you can clarify the lifetime of the string (which is what is causing
the problem):
- (BOOL)test:(BOOL)error
{
if (error) goto cleanup;
{
NSString* theString = [[NSString alloc] init];
//do stuff with theString;
}
cleanup:
return error;
}
and another:
- (BOOL)test:(BOOL)error
{
NSString* theString = [[NSString alloc] init];
if (error) goto cleanup;
//do stuff with theString;
cleanup:
return error;
}
I think the general rule is:
If you aren't going to use the allocated object past the goto target, put it in
a sub-block. If you need to use it after the goto target, allocate it prior to
the first goto.
Aaron
On Feb 2, 2013, at 11:55 AM, Jan E. Schotsman <[email protected]> wrote:
> Hello,
>
> Today I got an error message about goto and ARC, something about "leaving a
> protected area".
> Does this mean goto is nearly unusable under ARC?
>
> Jan E._______________________________________________
>
> Cocoa-dev mailing list ([email protected])
>
> 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:
> https://lists.apple.com/mailman/options/cocoa-dev/eeyore%40monsterworks.com
>
> This email sent to [email protected]
_______________________________________________
Cocoa-dev mailing list ([email protected])
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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]