Search Unity

Logical operators behave differently on Mono & IL2CPP

Discussion in 'Android' started by dabo248, Jul 22, 2019.

  1. dabo248

    dabo248

    Joined:
    Jul 1, 2019
    Posts:
    4
    I recently switched to IL2CPP instead of Mono to build my Android game. I tested it on a real device and immediately felt the improved performance.. and also bug occurred. A bug which just occurs on IL2CPP builds for Android (iOS works great..). After some investigation I could track it down to a logical operation in an if-statement, which behaves slightly differently on Mono and IL2CPP builds:

    Code (CSharp):
    1. int prevSection = GetPreviousSection();
    2. int curSection = GetSection();
    3.  
    4. Debug.Log(curSection != prevSection); // Prints True
    5. Debug.Log(HasSpawnedSectionEnd()); // Prints True.. call does not change any values
    6. Debug.Log(curSection != prevSection && !HasSpawnedSectionEnd()); // Prints True on IL2CPP, False on Mono
    That's how I assume it should be executed instead (based on C++ operator precedence):

    Code (Pseudo):
    1. curSection != prevSection && !HasSpawnedSectionEnd()
    2. ...
    3. true && !true
    4. ...
    5. true && false
    6. ...
    7. false
    Strangely the following code works as expected on IL2CPP:

    Code (CSharp):
    1. int prevSection = GetPreviousSection();
    2. int curSection = GetSection();
    3.  
    4. bool bool_1 = curSection != prevSection; // True
    5. bool bool_2 = !HasSpawnedSectionEnd(); // False
    6. bool bool_3 = bool_1 && bool_2; // False
    Did I miss something? Race conditions can be excluded, because HasSpawnedSectionEnd and both int values do not change over the process. It just occurs if 'C++ Compiler Configuration' is set on 'Release' or 'Master', not on 'Debug'.