Hi all,
I use the "check for success/goto failed" pattern everywhere, but
Clang refuses to compile such code with ARC enabled if the goto
bypasses a variable declaration (giving a "goto into protected scope"
error.)
Is this something that's likely to be fixed? Compiling the program
below triggers the error.
// compile with: clang -framework Foundation -fobjc-arc <file>.m
#import <Foundation/Foundation.h>
int main() {
goto cleanup;
NSString *str;
cleanup:
return 0;
}
If you put the variable in its own scope, preventing accidental access, you won't get the error:
int main() {
goto cleanup;
{
NSString *str;
}
cleanup:
return 0;
}
If you don't do this, you bypass the zero-initialization of 'str', and when ARC tries to clean it up, it'll end up trying to release some random bit of memory.
This should handle any valid case; if you want to use 'goto' but can't enclose the variables in their own scope, you'll have to move the declarations before the goto (as K&R intended *grin*).
Jordan
Note that (IIRC) goto can /leave/ a protected scope, so you can also just put one set of braces around the /rest/ of your function, and only leave the cleanup code outside. You won't need to nest new braces after every goto.
This restriction is not likely to be lifted; there are a number of challenges
with branches that enter scopes.
I assume your failure label is always at the end of the function; in that case,
you can wrap everything up to that point in curly braces, like so:
- (void) broadcastChanges {
{
if (self.error) goto failure;
NSString *foo = self.name;
[self broadcastName: foo];
return;
}
failure:
abort();
}
You might also consider moving to a structural programming pattern.
John.