Re: goto and ARC
Re: goto and ARC
- Subject: Re: goto and ARC
- From: Aaron Montgomery <email@hidden>
- Date: Sat, 02 Feb 2013 12:13:11 -0800
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@hidden> 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@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
_______________________________________________
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
References: | |
| >goto and ARC (From: "Jan E. Schotsman" <email@hidden>) |