# UnityEditor crashes on EventInstance callback

**URL:** https://qa.fmod.com/t/unityeditor-crashes-on-eventinstance-callback/17018
**Category:** Unity
**Tags:** unity
**Created:** [April 10, 2021, 7:09pm UTC](https://qa.fmod.com/t/unityeditor-crashes-on-eventinstance-callback/17018 "2021-04-10T19:09:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![verodicia](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/verodicia/32/2118_2.png) [@verodicia](https://qa.fmod.com/u/verodicia)
#### Post date: [April 10, 2021, 7:09pm UTC](https://qa.fmod.com/t/unityeditor-crashes-on-eventinstance-callback/17018/1 "2021-04-10T19:09:00Z")

</div>

Hello, IDK if this is the right place to ask, but I’m trying to make a class that plays in-game sfx using FMOD.Studio.EventInstance.

I store all the stopped instances in a stack, I push into the stack either when I’m creating a new Instance or inside OnEventInstanceStopped callback…

However the unity editor keeps crashing in OnEventInstanceStopped, I don’t get any logs out of the function so I believe it crashes when calling it?

```
public class AudioEventFXInstance
{
    private List<FMOD.Studio.EventInstance> instances = new List<FMOD.Studio.EventInstance>();
    private Stack<FMOD.Studio.EventInstance> stoppedInstances = new Stack<FMOD.Studio.EventInstance>();
    private IntPtr stackPtr;

    private Transform transform = null;
    private string path = "";

    public AudioEventFXInstance(Transform transform, string path)
    {
        this.transform = transform;
        this.path = path;

        stackPtr = GCHandle.ToIntPtr(GCHandle.Alloc(stoppedInstances));

        AddInstance();
    }

    ~AudioEventFXInstance()
    {
        foreach (FMOD.Studio.EventInstance instance in instances)
            instance.release();
    }

    private void AddInstance()
    {
        FMOD.Studio.EventInstance instance = FMODUnity.RuntimeManager.CreateInstance(path);
        instance.setCallback(OnEventInstanceStopped, FMOD.Studio.EVENT_CALLBACK_TYPE.STOPPED);
        instance.setUserData(stackPtr);

        instances.Add(instance);
        stoppedInstances.Push(instance);
    }

    private FMOD.RESULT OnEventInstanceStopped(FMOD.Studio.EVENT_CALLBACK_TYPE type, IntPtr _event, IntPtr parameters)
    {
        FMOD.Studio.EventInstance instance = new FMOD.Studio.EventInstance(_event);

        IntPtr data;
        instance.getUserData(out data);
        
        Stack<FMOD.Studio.EventInstance> stack = (GCHandle.FromIntPtr(data).Target as Stack<FMOD.Studio.EventInstance>);
        
        return FMOD.RESULT.OK;
    }

    public void Start()
    {
        FMOD.Studio.EventInstance instance;

        if (stoppedInstances.Count == 0)
            AddInstance();
            
        instance = stoppedInstances.Pop();

        FMODUnity.RuntimeManager.AttachInstanceToGameObject(instance, transform, (Rigidbody2D)null);
        instance.start();
    }
    
}
```

---

<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: [April 15, 2021, 3:50am UTC](https://qa.fmod.com/t/unityeditor-crashes-on-eventinstance-callback/17018/2 "2021-04-15T03:50:52Z")

</div>

What versions of FMOD and Unity are you using?

When I try your script I do get:

```auto
UnityException: FindObjectsOfTypeAll is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead.

```

Which is due to the `FMODUnity.RuntimeManager` being called (and created) in the constructor, if the script is attached to a gameObject.

Are you able to get any meaningful info from the crash logs at all?

---

<div class="post-metadata">

### Author: ![verodicia](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/verodicia/32/2118_2.png) [@verodicia](https://qa.fmod.com/u/verodicia)
#### Post date: [April 20, 2021, 6:14am UTC](https://qa.fmod.com/t/unityeditor-crashes-on-eventinstance-callback/17018/3 "2021-04-20T06:14:33Z")

</div>

Hello, Sorry for the late response, I’m unable to upload the crash log files here ([Crash\_2021-04-19\_153046519.rar - Google Drive](https://drive.google.com/file/d/15CcK77dihHx-TJ_U-M4H6tZe8ofb-rl8/view?usp=sharing)).

> Unknown caused an Access Violation (0xc0000005)  
> in module Unknown at 0033:611109c1.

I’m using Unity 2019.4.22.f1 LTS release, I believe my FMOD version is 2.0.08, 64x, Build#114339.

Apologies for not providing the full source code, you can find the unity project here: [GitHub - Light7734/Unity-Managers: A project with manager classes and tidy folder structure to start your 2d project with.](https://github.com/Light3039/Unity-Managers)  
(Audio scripts are in Assets/Scripts/Audio/…)

You can crash the editor by spamming the 3 buttons in the middle, it crashes randomly when a sound stops.

I don’t get the UnityException you’ve mentioned, I think it’s because I use this class to create AudioEventFXInstance:  
I’m not sure whether or not my appraoch is corerct.

```
public class AudioEmitterFX : MonoBehaviour
{
    [SerializeField] 
    private AudioEmitterData data; // struct that holds an array of string(fmod event)

    private Dictionary<string, AudioEventFXInstance> events = new Dictionary<string, AudioEventFXInstance>();

    private void Awake()
    {
        for (int i = 0; i < data.events.Length; i++)
        {
            events[data.events[i].Substring(data.events[i].LastIndexOf("/") + 1)] = new AudioEventFXInstance(transform, data.events[i]);
            Debug.Log(data.events[i] + " - > " + data.events[i].Substring(data.events[i].LastIndexOf("/") + 1));
        }
        Debug.Log(events);
    }

    public AudioEventFXInstance this[string name]
    {
        get { return events[name]; }
    }
}

```

---

<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: [April 22, 2021, 1:00am UTC](https://qa.fmod.com/t/unityeditor-crashes-on-eventinstance-callback/17018/4 "2021-04-22T01:00:51Z")

</div>

This appears to be cause by the callback delegate being garbage collected, because it is not being cached the garbage collector will come along (at a seemingly random time) and it will appear to be unreferenced so it will be removed.

In general with callbacks, we recommend doing three things:

- Cache the delegate, this will stop the garbage collector from removing the callback.

- Make callback functions static.

- Functions require the `[MonoPInvokeCallback]` attribute.

This is mentioned in the [Platform Specifics (callbacks on ahead of time compile platforms)](https://fmod.com/resources/documentation-unity?version=2.2&page=platform-specifics.html#callbacks-on-ahead-of-time-compile-platforms) section in our docs.
