Unity integration may be crashing Unity editor if a callback references Unity GameObject

I’m experiencing Unity editor crashes with which FMOD may have something to do.

Using EventInstance::setCallback, I wrote a delegate which lets the UI element pulse via Animator.

    public StudioEventEmitter bgmEvent;
    [SerializeField] private Animator uiAnimator;
    private EVENT_CALLBACK beatPulseCallback;

    private void Start() {
        beatPulseCallback = StudioEventCallBack;
        bgmEvent.EventInstance.setCallback(
            beatPulseCallback, EVENT_CALLBACK_TYPE.TIMELINE_BEAT
        );
    }
    private RESULT StudioEventCallBack(EVENT_CALLBACK_TYPE type, EventInstance eventInstance, IntPtr parameters)
    { 
        Debug.Log("CB");
        if (uiAnimator != null)
        {
            uiAnimator.SetTrigger("PulseTriggered");
        }
        return RESULT.OK;
    }

Note that the callback references an Animator during the procedure. This code appears to work until I exit the PlayMode when Unity editor crashes.
If I trigger the same code using other means (e.g. KeyDown,) I cannot observe any crashes.

The project that has this code is quite big, and the FMOD integration takes time to shut down when exiting the PlayMode, so I did some experiments.


When I move this code to a new empty project and try to reproduce it:

  • 70% of the time it doesn’t fail
  • 20% of the time it freezes the Unity editor, and
  • 10% of the time the code crashes it.

When it freezes the editor, it usually leaves a NullReferenceException before its demise. The funny thing is that the exception occurs at if (uiAnimator != null) part - the null check itself is returning an NRE, which should not happen.

Then I added an OnDestroy() method to remove the callback before quitting:

    private void OnDestroy() {
        bgmEvent.EventInstance.setCallback(null);
        bgmEvent.Stop();
    }

And this code seems to mitigate the crash altogether (and that’s what I currently do to avoid it.) However, when I stop the event before removing callbacks, the editor crashes.


Based on the observations, I suspect that the callback can be being triggered after the Unity context is destroyed - and if the callback happens to reference GameObjects, everything goes haywire since the underlying reference is gone.

Can you confirm if my suspicion is correct? If it is, I consider this issue a bug (or at least should be documented somehow? AFAIR there is no mention of the danger of referencing GameObjects in callbacks - this example does not seem to clear callbacks in OnDestroy method.)

OS: macOS Catalina 10.15.3.
Unity version: 2019.3.3f1.
FMOD Studio/Unity Integration version: 2.00.07.

I have not been able to reproduce the issue here, but it may have something to do with the callback function. You may need to add MonoPInvokeCallbackAttribute as a custom attribute and make the function static.

Currently, only static methods can be passed as delegates to the native code.

https://docs.unity3d.com/Manual/TroubleShootingIPhone.html