# IL2CPP Crash on GCHandle.Alloc for callbacks

**URL:** <https://qa.fmod.com/t/il2cpp-crash-on-gchandle-alloc-for-callbacks/17773>\
**Category:** Unity\
**Tags:** switch, unity\
**Created:** [October 10, 2021, 8:29pm UTC](https://qa.fmod.com/t/il2cpp-crash-on-gchandle-alloc-for-callbacks/17773 "2021-10-10T20:29:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kim1](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/kim1/32/67_2.png) [@kim1](https://qa.fmod.com/u/kim1)\
**Post date:** [October 10, 2021, 8:29pm UTC](https://qa.fmod.com/t/il2cpp-crash-on-gchandle-alloc-for-callbacks/17773/1 "2021-10-10T20:29:42Z")

</div>

IL2CPP Crash on GCHandle.Alloc for callbacks

**Unity: 2021.1.23f1**  
**FMOD: 2.02.03**  
**Scripting Backend: IL2CPP**

Works great in editor.  
Crashed on compiled.  
Works great with Mono

ArgumentException: Object contains non-primitive or non-blittable data.

Example code.

```auto
FMOD.Studio.EventInstance eventInstance = FMODUnity.RuntimeManager.CreateInstance("event:/example");

TimelineInfo timelineInfo = new TimelineInfo();

// this will crash the IL2CPP compiler
GCHandle timelineHandle = GCHandle.Alloc(timelineInfo, GCHandleType.Pinned); 
eventInstance.setUserData(GCHandle.ToIntPtr(timelineHandle));

FMOD.Studio.EVENT_CALLBACK beatCallback = new FMOD.Studio.EVENT_CALLBACK(EventCallback);
eventInstance.setCallback(beatCallback, FMOD.Studio.EVENT_CALLBACK_TYPE.TIMELINE_MARKER);

FMOD.RESULT EventCallback(
		FMOD.Studio.EVENT_CALLBACK_TYPE type, 
		IntPtr instancePtr, 
		IntPtr parameterPtr
){
// do work on event here..
return FMOD.RESULT.OK;
}

```

Is there an alternative way to get timeline callbacks without GCHandle.Alloc ?

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [October 13, 2021, 12:08am UTC](https://qa.fmod.com/t/il2cpp-crash-on-gchandle-alloc-for-callbacks/17773/2 "2021-10-13T00:08:02Z")

</div>

It looks like you are using the 2.00 timeline example with the 2.02 integration- can you try using the [2.02 timeline example](https://fmod.com/resources/documentation-unity?version=2.02&page=examples-timeline-callbacks.html) instead? One change is that we no longer recommend pinning the memory with `GCHandleType.Pinned` because it was causing some issues on iOS, but we haven’t encountered this with IL2CPP.

---

<div class="post-metadata">

**Author:** ![kim1](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/kim1/32/67_2.png) [@kim1](https://qa.fmod.com/u/kim1)\
**Post date:** [October 13, 2021, 10:56am UTC](https://qa.fmod.com/t/il2cpp-crash-on-gchandle-alloc-for-callbacks/17773/3 "2021-10-13T10:56:59Z")

</div>

Can confirm 2.02 timeline example doesn’t crash on IL2CPP for various platforms.  
seeing some marshal errors but might be unrelated

To marshal a managed method, please add an attribute named ‘MonoPInvokeCallback’ to the method definition.

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [October 13, 2021, 9:48pm UTC](https://qa.fmod.com/t/il2cpp-crash-on-gchandle-alloc-for-callbacks/17773/4 "2021-10-13T21:48:39Z")

</div>

Thanks for confirming- does your callback method (`EventCallback` from your above snippet) have this attribute added above it?

```auto
[AOT.MonoPInvokeCallback(typeof(FMOD.Studio.EVENT_CALLBACK))]

```

---

<div class="post-metadata">

**Author:** ![kim1](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/kim1/32/67_2.png) [@kim1](https://qa.fmod.com/u/kim1)\
**Post date:** [October 17, 2021, 3:37pm UTC](https://qa.fmod.com/t/il2cpp-crash-on-gchandle-alloc-for-callbacks/17773/5 "2021-10-17T15:37:06Z")

</div>

looks like this

```auto
[AOT.MonoPInvokeCallback(typeof(FMOD.Studio.EVENT_CALLBACK))]
    static FMOD.RESULT BeatEventCallback2(FMOD.Studio.EVENT_CALLBACK_TYPE type, IntPtr instancePtr, IntPtr parameterPtr){

```

Calling a singelton on end that crash the program

```auto
case FMOD.Studio.EVENT_CALLBACK_TYPE.STOPPED:
					{
						Debug.Log("Song ended event called");
						RythmManager.instance.OnSongEnd();
					}
				break;

```

Errors like

```auto
WindowsPlayer UnityException: IsObjectMonoBehaviour can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

```

```auto
UnityException: get_isActiveAndEnabled can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

```

What is the best way to get back to the main thread from a

```auto
[AOT.MonoPInvokeCallback(typeof(FMOD.Studio.EVENT_CALLBACK))]

```

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [October 18, 2021, 12:57am UTC](https://qa.fmod.com/t/il2cpp-crash-on-gchandle-alloc-for-callbacks/17773/6 "2021-10-18T00:57:34Z")

</div>

Hmm, there are probably some sophisticated .NET ways of dealing with this, but the simplest way I can think of would be to call `RythmManager.instance.OnSongEnd()` from `Update()`, and use an atomic operation to trigger it from your callback. Something like:

```auto
case FMOD.Studio.EVENT_CALLBACK_TYPE.STOPPED:
    {
        Debug.Log("Song ended event called");
        onSongEndCalled = true; // Atomic primitive type assignment
    }
    break;

```

And then in `Update()`

```auto
if(onSongEndCalled)
{
    RythmManager.instance.OnSongEnd();
    onSongEndCalled = false;
}

```

Should work around that error.
