Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(iOS) Jump bypasses initialization of retaining variable when using goto

I got this compiler error when using goto in my ARC iOS project.

Cannot jump from this goto statement to its label. Jump bypasses initialization of retaining variable

I know goto is bad in general but ... please just tell me how to fix it. The codes are like following,

//some process
NSArray *current = ... ;
if (current.count ==0) goto cleanup;
//proceed to next
if (processed failed) goto cleanup;
//further process

cleanup:
//clean up codes
like image 410
Qiulang 邱朗 Avatar asked Oct 15 '25 15:10

Qiulang 邱朗


1 Answers

I finally figured it out! Actually the warning said it clearly, "Jump bypasses initialization of retaining variable" so in the section next

//In proceed to next section I declare & init some object!

My codes/problem is basically the same as c99 goto past initialization

The solution is simple, just added a {} block to it, as here mentioned Why can't variables be declared in a switch statement?

For those who are wondering why I still need goto, I think this explained it Is it ever advantageous to use 'goto' in a language that supports loops and functions? If so, why? , especially "Cleanly exiting a function", check here for an example http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c

Without goto mainline code is deep inside the nested conditions (Of course we can also introduce a helper function to deal with it).

like image 137
Qiulang 邱朗 Avatar answered Oct 18 '25 06:10

Qiulang 邱朗



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!