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

**URL:** <https://qa.fmod.com/t/unity-integration-may-be-crashing-unity-editor-if-a-callback-references-unity-gameobject/15501>\
**Category:** Unity\
**Created:** [February 28, 2020, 9:32am UTC](https://qa.fmod.com/t/unity-integration-may-be-crashing-unity-editor-if-a-callback-references-unity-gameobject/15501 "2020-02-28T09:32:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![clpsplug](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/clpsplug/32/851_2.png) [@clpsplug](https://qa.fmod.com/u/clpsplug)\
**Post date:** [February 28, 2020, 9:32am UTC](https://qa.fmod.com/t/unity-integration-may-be-crashing-unity-editor-if-a-callback-references-unity-gameobject/15501/1 "2020-02-28T09:32:30Z")

</div>

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.

```auto
    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:

```auto
    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](https://www.fmod.com/resources/documentation-unity?version=2.0&page=examples-timeline-callbacks.html) does not seem to clear callbacks in `OnDestroy` method.)

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

```

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [March 9, 2020, 11:43pm UTC](https://qa.fmod.com/t/unity-integration-may-be-crashing-unity-editor-if-a-callback-references-unity-gameobject/15501/2 "2020-03-09T23:43:07Z")

</div>

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.

> **[Unity - Manual: Troubleshooting on iOS devices](https://docs.unity3d.com/Manual/TroubleShootingIPhone.html)**

---

<div class="post-metadata">

**Author:** ![WillYuum](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/willyuum/32/6224_2.png) [@WillYuum](https://qa.fmod.com/u/WillYuum)\
**Post date:** [February 18, 2024, 4:26am UTC](https://qa.fmod.com/t/unity-integration-may-be-crashing-unity-editor-if-a-callback-references-unity-gameobject/15501/3 "2024-02-18T04:26:11Z")

</div>

I can approve to this message for I have experience the same thing and the only way is through handling the `StudioEventCallBack` with a static keyword.  
Worth the pain to try and break the norm :))
