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 <jesc...@xs4all.nl> 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 (Cocoa-dev@lists.apple.com)
> 
> 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 eey...@monsterworks.com


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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 arch...@mail-archive.com
  • goto and ARC Jan E. Schotsman
    • Re: goto and ARC Aaron Montgomery

Reply via email to