Infinite loop bug

Hello!
Today we found very annoying bug. It reproduces only on Windows Phone 8 device when you run it not from Visual Studio.
We created this minimalistic example:

using UnityEngine;

public class Main : MonoBehaviour {

    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 500, 500), "Loop me"))
        {
            Debug.LogError("You click me");
            int i = 0;
            while (i < 0 || i < 1)
            {
                i++;
            }
            Debug.LogError("I am okay");
        }
    }
}

The main line here is while (i < 0 || i < 1)
This application will freeze, when you click button. Probably, this line causes infinite loop by some reason.
Instead of i < 1 you can write something another that will evaluate to True at first time(j < 10 for example).

Bytecode generated by Unity does not contain bugs, maybe it is a bug in CLR . We made similiar native Windows Phone project and everything worked fine.

Have you any ideas what causes this bug?

Have you tried debugging it with Visual Studio? To make sure it really gets stuck in this loop.

If we debug it with Visual Studio everething works fine, but if you deploy to device and launch from user interface then bug appeares.

I’ll try it locally. Which Unity version are you using?

We are using 4.3.2f1.

I reproduced it locally. I’ll investigate it and let you know what I find.

Thank you. I rewrote this code in my project, but it will be interesting to know.

Looks like the bug isn’t in Unity, since I managed to reproduce it in an empty windows phone application (without Unity). I have forwarded it to Microsoft.

Hi,

I got response from Microsoft. It looks like it is indeed a bug in JIT optimizer. Until it is fixed, you can mark affected methods with this attribute to work around the issue:

[MethodImpl(MethodImplOptions.NoOptimization)]