How do you handle 'require( …, bail)' statements with ARC?

帅比萌擦擦* 提交于 2019-12-08 19:20:49

问题


I'm looking through some of the sample code for the Square Cam in Apple's sample code. I want to replicate some of it's functionality in a modern project using ARC. However, there are a ton of require statements such as:

BOOL success = (destination != NULL);
require(success, bail);

Which generates the compiler error:

Goto into protected scope.

My question is -- what is the appropriate way to handle such statements in a project using ARC?


回答1:


I had the same problem (with the same sample code). The code looked like this:

BOOL success = (destination != NULL);
require(success, bail);

//Initialise some variables

bail:
//Deal with errors

I added braces around the block with the declarations to make their scope clear:

BOOL success = (destination != NULL);
require(success, bail);
{
    // *** Initialise some variables ***
}
bail:
{
    //Deal with errors
}

And it solved the problem for me. Through looking at this I also learned you can sometimes expand build errors to get more detail.




回答2:


Apparently bail is in a scope with one or more __block variables; this is not allowed. See http://clang.llvm.org/compatibility.html#blocks-in-protected-scope for more. The solution proposed there is to limit the scope of the __block variable(s), by putting them in brace-delimited blocks. This may not work always; YMMV.




回答3:


I am adding some description for @Lewis42 answer.

If you don't put variable in its own scope, you bypass the initialisation of all variable after goto, and when ARC tries to clean it up, it will end up trying to release some random bit of memory.

If you don't want to put variables in their own scope make sure that any variable should not be declared below goto keyword.

Jumps to within __block variable scope

__block require special runtime initialization. A jump into the scope of a __block variable bypasses this initialization, leaving the variable's metadata in an invalid state.



来源:https://stackoverflow.com/questions/11388853/how-do-you-handle-require-bail-statements-with-arc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!