calling Destroy() on UnityEvent

I have some code that is creating a default inspector for a MonoBehaviour which has a UnityEvent:

void GUIWindowCallBack(int windowID)
{
     Editor e = Editor.CreateEditor(someMonoBehaviourWithUnityEvent);
     e.DrawDefaultInspector();
      DestroyImmediate(e);
}

And I keep getting this error:
NullReferenceException: SerializedObject of SerializedProperty has been Disposed.

If I try to draw an inspector for a MonoBehaviour without a UnityEvent, it works great.
If I don’t call DestroyImmediate, it works but I end up leaking thousands of objects.
Any help or direction would be much appreciated.
Thanks!

is it possible to destroy the editor from the class Destructor ~?

using System;

class Example
{
 public Example()
 {
  Console.WriteLine("Constructor");
 }

 ~Example()
 {
  Console.WriteLine("Destructor");
 }
}

Thanks for the response!
Disposing the item isn’t really the problem. It’s when I try to create a new editor.
For example:

Here I create an editor, dispose of it, then create another editor. This seems to work with any MonoBehaviour that doesn’t have a UnityEvent:

Editor e1 = Editor.CreateEditor(someMonoBehaviourWithUnityEvent);
e1.DrawDefaultInspector();//<--- works fine
DestroyImmediate(e1);

Editor e2 = Editor.CreateEditor(someMonoBehaviourWithUnityEvent);
e2.DrawDefaultInspector();//<--- NullReferenceException: SerializedObject of SerializedProperty has been Disposed.
DestroyImmediate(e2);

But when I have a MonoBehaviour with a UnityEvent, it throws an exception where I described in the code.
To try to pinpoint the issue, I wrote a custom editor for the MonoBehaviour that would try to access the UnityEvent serialized property and create a inspector for it:

[CustomEditor (typeof(CallFunctionNode))]
    public class CallFunctionEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            CallFunctionNode node = (CallFunctionNode)target;
            SerializedProperty prop = serializedObject.FindProperty("function");

            EditorGUIUtility.LookLikeControls();
            EditorGUILayout.PropertyField(prop);//<--- NullReferenceException: SerializedObject of SerializedProperty has been Disposed.

            if(GUI.changed)
            {
                serializedObject.ApplyModifiedProperties();
            }
        }
    }

So to the best of my knowledge, when I dispose the editor for the MonoBehaviour, it is somehow disposing the UnityEvent as well.

did you try moving the Editor e1/e2 declaration outside of the function, and make it a class variable?
could be e1/e2 gets destroyed when the function GUIWindowCallBack() returns.

Plus, you have to remember, Unity’s imGUI makes multiple passes before it starts to draw. :expressionless: