EditorGUIUtility.ShowObjectPicker throws InvalidOperationException

The Full Stack Trace:

InvalidOperationException: Operation is not valid due to the current state of the object
System.Collections.Stack.Peek () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections/Stack.cs:321)
UnityEngine.GUILayoutUtility.EndLayoutGroup () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GUILayoutUtility.gen.cs:223)
UnityEngine.GUILayout.EndHorizontal () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GUILayout.gen.cs:244)
UnityEditor.EditorGUILayout.EndHorizontal () (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:6426)
RPG.Editor.Items.ItemDatabaseEditor.DrawDetails () (at Assets/Scripts/Editor/Items/ItemDatabaseEditor.cs:186)
RPG.Editor.Items.ItemDatabaseEditor.OnGUI () (at Assets/Scripts/Editor/Items/ItemDatabaseEditor.cs:64)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)

The offending source code:

if (GUILayout.Button(RPG.Utils.TextureUtils.GetTextureFromSprite(m_selectedItem.m_sprite), GUILayout.Width(64.0f), GUILayout.Height(64.0f)))
{
     int controlID = EditorGUIUtility.GetControlID(FocusType.Passive);
     EditorGUIUtility.ShowObjectPicker<Sprite>(m_selectedItem.m_sprite, false, "", controlID);
}

This is part of a large file, I pulled pulled out the code that throws the error, I know this throws the error because commenting out the EditorGUIUtility.ShowObjectPicker stops the error.

I would really like to make this error go away, but I am 99.9% sure it is not in my code however.
I thought it might be an unclosed Layout, but, I always use braces on them and the counts match.

Here is an example that will also cause the error, an interesting thing to point out is if you comment out the Begin and End the error will not happen.

using UnityEngine;
using UnityEditor;
using System.Collections;

public class TestEditor : UnityEditor.EditorWindow
{
    [MenuItem("Editors/Test Editor")]
    public static void Open()
    {
        TestEditor window = EditorWindow.GetWindow<TestEditor>();
        window.minSize = new Vector2(200, 100);
        window.Show();
    }

    void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();

        if (GUILayout.Button("Test Button", GUILayout.Width(64.0f), GUILayout.Height(64.0f)))
        {
            int controlID = EditorGUIUtility.GetControlID(FocusType.Passive);
            EditorGUIUtility.ShowObjectPicker<Sprite>(null, false, "", controlID);
        }

        if (Event.current.commandName == "ObjectSelectorUpdated")
        {
            Sprite sprite = EditorGUIUtility.GetObjectPickerObject() as Sprite;
            this.Repaint();
        }

        EditorGUILayout.EndHorizontal();
    }
}

The full source for the file can be found Here, however you will not be able to run it. I have provided a code sample that reproduces the error on my machine above.

There’s a known problem with the OS triggering a repaint of the host window immediately after the object picker window is shown, but before the previous input event had been handled completely. This triggers a new OnGUI call before the previous one had ended. Unfortunately, our IMGUI layout system does not handle this nicely.

Fortunately, it 99.9% of cases, the rest of the input event OnGUI call is not important, because you’ve already handled the event to show the object picker. You can call GUIUtility.ExitGUI() immediately after ShowObjectPicker to have the OnGUI method stop execution immediately. This will fix your problem for now.

It’s on our todo list to better workaround the OS event behavior.