Search Unity

  1. How has 2019.2 and the beta been for you so far? Give us feedback in this thread.
    Dismiss Notice

Mac OS: Various GUI exceptions after OS dialogs have been shown

Discussion in '2018.1 Beta' started by SoxwareInteractive, Feb 2, 2018.

  1. SoxwareInteractive

    SoxwareInteractive

    Joined:
    Jan 31, 2015
    Posts:
    357
    Hi,
    there are several situations where the Unity Editor throws exceptions from within the GUI system after a Mac OS dialog (e.g. a Message Box, a Save File dialog,...) was shown. If and which exception is thrown seems to depend on the surrounding GUI code. For me it looks like the IMGUI stack get's destroyed after such a Mac OS dialog was shown thus making the following GUI function throw an exception.

    Please note that none of the following problems happens on Windows.

    Case 996419: Mac OSX: GUI exception after showing MessageBox (in Popup)
    Reproduced in Unity 2018.1.0b05.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class Test : EditorWindow
    7. {
    8.     // Add menu named "My Window" to the Window menu
    9.     [MenuItem("Window/TestWindow")]
    10.     static void Init()
    11.     {
    12.         // Get existing open window or if none, make a new one:
    13.         Test window = (Test)EditorWindow.GetWindow(typeof(Test));
    14.         window.Show();
    15.     }
    16.  
    17.     private int selectedIndex = 0;
    18.  
    19.     private void OnGUI()
    20.     {
    21.         GUILayout.Space (4);
    22.  
    23.         GUILayout.BeginHorizontal();
    24.         {
    25.             int newIndex = EditorGUILayout.Popup (selectedIndex, new string[] { "Test1", "Test2" });
    26.  
    27.             if (newIndex != selectedIndex)
    28.             {
    29.                 selectedIndex = newIndex;
    30.  
    31.                 EditorUtility.DisplayDialog ("Test", "Test", "Ok");
    32.             }
    33.         }
    34.         GUILayout.EndHorizontal ();
    35.  
    36.         GUILayout.Space (4);
    37.     }
    38. }
    Bildschirmfoto 2018-02-02 um 09.13.25.png

    Case 996426: EditorGUI exceptions on MacOS
    Reproduced in Unity 2017.3.0f3 (seems to be fixed in Unity 2018.1.0b05).
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class Test : EditorWindow
    7. {
    8.     // Add menu named "My Window" to the Window menu
    9.     [MenuItem("Window/TestWindow")]
    10.     static void Init()
    11.     {
    12.         // Get existing open window or if none, make a new one:
    13.         Test window = (Test)EditorWindow.GetWindow(typeof(Test));
    14.         window.Show();
    15.     }
    16.  
    17.     void OnGUI()
    18.     {
    19.         GUILayout.BeginScrollView(Vector2.zero, GUIStyle.none, GUIStyle.none);
    20.         {
    21.             GUILayout.BeginHorizontal();
    22.             {
    23.                 if (GUILayout.Button ("Test1"))
    24.                 {
    25.                     // EditorUtility.SaveFilePanelInProject("Test", "test_file", "asset", "Please enter a file name");
    26.                    
    27.                     EditorUtility.DisplayDialog ("Test", "bkaldkslj", "OK");
    28.                 }
    29.             }
    30.             GUILayout.EndHorizontal ();
    31.         }
    32.         GUILayout.EndScrollView();
    33.  
    34.  
    35.         GUILayout.Space (10);
    36.         GUILayout.BeginHorizontal();
    37.         {
    38.             if (GUILayout.Button ("Test2"))
    39.             {
    40.                 //EditorUtility.SaveFilePanelInProject("Test", "test_file", "asset", "Please enter a file name");
    41.                
    42.                  EditorUtility.DisplayDialog ("Test", "bkaldkslj", "OK");
    43.             }
    44.         }
    45.         GUILayout.EndHorizontal ();
    46.  
    47.  
    48.  
    49.         GUILayout.BeginVertical ();
    50.             GUILayout.BeginHorizontal();
    51.             {
    52.                 if (GUILayout.Button ("Test3"))
    53.                 {
    54.                     //EditorUtility.SaveFilePanelInProject("Test", "test_file", "asset", "Please enter a file name");
    55.                    
    56.                     EditorUtility.DisplayDialog ("Test", "bkaldkslj", "OK");
    57.                 }
    58.             }
    59.             GUILayout.EndHorizontal ();
    60.         GUILayout.EndVertical ();
    61.  
    62.  
    63.         GUILayout.BeginHorizontal();
    64.         {
    65.             if (GUILayout.Button ("Test4"))
    66.             {
    67.                 //EditorUtility.SaveFilePanelInProject("Test", "test_file", "asset", "Please enter a file name");
    68.                
    69.                 EditorUtility.DisplayDialog ("Test", "bkaldkslj", "OK");
    70.             }
    71.         }
    72.         GUILayout.EndHorizontal ();
    73.  
    74.         GUILayout.Space (4);
    75.     }
    76. }
    77.  
    Bildschirmfoto 2018-02-02 um 09.37.17.png

    Case 996431: Editor GUI exception on Mac OS when SaveDialog is opened during a Layout event
    Reproduced in Unity 2017.3.0f3 (seems to be fixed in Unity 2018.1.0b05).
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEditor;
    6.  
    7. public class Test : EditorWindow
    8. {
    9.     // Add menu named "My Window" to the Window menu
    10.     [MenuItem("Window/TestWindow")]
    11.     static void Init()
    12.     {
    13.         // Get existing open window or if none, make a new one:
    14.         Test window = (Test)EditorWindow.GetWindow(typeof(Test));
    15.         window.Show();
    16.     }
    17.  
    18.     private Action guiDelayed = null;
    19.  
    20.     private void Foo()
    21.     {
    22.         EditorUtility.SaveFilePanelInProject("Test", "test_file", "asset", "Please enter a file name");
    23.     }
    24.  
    25.     void OnGUI()
    26.     {
    27.         // Delayed Function Calls
    28.         if ((Event.current.type == EventType.Layout) && (guiDelayed != null))
    29.         {
    30.             Action temp = guiDelayed;
    31.             guiDelayed = null;
    32.             temp();
    33.         }
    34.  
    35.         if (GUILayout.Button("Test"))
    36.         {
    37.             guiDelayed = Foo;
    38.         }
    39.     }  
    40. }
    41.  

    Bildschirmfoto 2018-02-02 um 09.47.01.png

    It looks like some problems have been already fixed in Unity 2018.1. For us Asset Store publishers, these bugs are really critical as it makes Editor Extensions that use OS dialogs throw quite a lot of exceptions on Mac OS. Please note that not only Unity 2017.3 seems to be affected. Even older generations seem to have that issue. I hope that this problems will also be fixed in Unity 2017.3 and below.

    Please provide some information on what is happening here and if there is any workaround available. It would also be interesting to know which versions are affected, so that we can implement warning messages that inform our users if they use our editor extension in an infected Unity version.

    Thank you very much!
     
    MrEsquire and SonicBloomEric like this.
  2. SoxwareInteractive

    SoxwareInteractive

    Joined:
    Jan 31, 2015
    Posts:
    357
    I corresponded with Unity QA the last two days. Big thanks to how fast they reacted. This is what we came up with:
    • Case 996431 and Case 996426 should be fixed in Unity 2017.3.1.
    • Case 996419 will be fixed in the 2018.1 release cycle. If I remember correctly, this one also appeared in the Unity 2017 generation but I'm not sure if it will also be fixed there.
     
  3. MrEsquire

    MrEsquire

    Joined:
    Nov 5, 2013
    Posts:
    2,712
    Thank you for logging the issues, I come across a few of these sometimes and ignore them.
    The macOS functionality needs some improvements so compatibility is good with all new releases.
     
  4. Ludiq

    Ludiq

    Joined:
    Mar 6, 2015
    Posts:
    555
    Bumping this thread, do you have any update from Unity QA? My users are still frequently reporting 996419 (NullReferenceException in Space() or FlexibleSpace()).
     
  5. SoxwareInteractive

    SoxwareInteractive

    Joined:
    Jan 31, 2015
    Posts:
    357
    If I remember correctly Unity QA finished there work on this with the following result:
    Case 996419: Not going to be fixed for an unknown reason.
    Case 996426 and 996431: Fixed in 2017.3.1f1

    There are still quite a few situations where you get similar exceptions across the Unity versions I support with UMotion. So this is a general problem and should not be treated case by case...
    As Unity usually won't update older versions of Unity anyway (that I still want to support) I decided to spend some time on a workaround based on a suggestion @Mikilo made in another thread:

    Basically I wrap all Space(), FlexibleSpace(), EndHorizontal() and EndVertical() functions and put a try-catch in there. Then if I catch an exception, I call GUIUitlitly.ExitGUI(). Example:

    Code (CSharp):
    1. public static void SpaceWrapped(float pixels)
    2. {
    3.    try
    4.    {
    5.       GUILayout.Space(pixels);
    6.    }
    7.    catch
    8.    {
    9.       GUIUtility.ExitGUI();
    10.    }
    11. }
    Seems to work pretty well in my case, but please note that ExitGUI() will immediately stop GUI code execution from where it was called. This might change your code flow and produce different behavior on Mac than on Windows.

    Hope this helps.
     
  6. Ludiq

    Ludiq

    Joined:
    Mar 6, 2015
    Posts:
    555
    Thanks for the workaround, but this is just ridiculous... We shouldn't have to wrap official API methods in try/catch blocks because Unity doesn't guarantee they might not throw an undocumented NullReferenceException for unknown reasons (... and refuse to fix it!?)

    Do you have a link to case 996419? Can we re-open it?
     
  7. SoxwareInteractive

    SoxwareInteractive

    Joined:
    Jan 31, 2015
    Posts:
    357
    Yep...

    Here's the link:
    https://issuetracker.unity3d.com/is...e-is-called-after-guilayout-dot-endhorizontal

    I don't think that reopening this single case (and maybe fixing it) will change much. There are still other situations where you get this type of exceptions. I stopped tracing each of them down to a reproduce able test case because this is obviously a general problem that needs to be addressed as such (remember that all these bugs happen after you return from a Mac OS dialog like a message box or something similar).
    Anyway if you want to support older Unity versions I'm afraid there is no way around using the workaround.
     
  8. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    1,455
    Hi,
    We decided to close 996419 as won’t fix for now. After assessing it’s severity we came to the conclusion that there are currently other more pressing issues in our backlog demanding our attention than we will be able to resolve in the foreseeable future. That doesn’t necessarily mean that this bug will never be fixed, as we reassess cases like these on a regular basis.
     
    SoxwareInteractive likes this.
  9. Ludiq

    Ludiq

    Joined:
    Mar 6, 2015
    Posts:
    555
    Hi Leonhard, thanks for clarifying. I'm sorry, I overreacted thinking the "Won't fix" label meant "Won't ever fix" instead of "Won't fix for reported version". I'll use the suggested workaround in the mean time to prevent errors for my users.
     
    LeonhardP likes this.