Il2CppInterop icon indicating copy to clipboard operation
Il2CppInterop copied to clipboard

InvalidOperationException when using delegates

Open PieKing1215 opened this issue 2 years ago • 1 comments

I'm trying to add a listener to a delegate, but I get an exception when the event runs.

BepInEx 6.0.0-be.665 Game is Unity 2022.1, v29 metadata (BepInEx says Running under Unity 2021.2.0f1 though?)

Setup:

// GameEvent is `public delegate void GameEvent();` (class that extends `Il2CppSystem.MulticastDelegate`)
// Plugin.OnRoundBegin is `public static void OnRoundBegin() {}`

GameEvents.OnRoundBegin += (GameEvents.GameEvent)Plugin.OnRoundBegin;
// or
GameEvents.OnRoundBegin += (System.Action)Plugin.OnRoundBegin;

Error when the event happens:

[Error  :Il2CppInterop] Exception in IL2CPP-to-Managed trampoline, not passing it to il2cpp: System.InvalidOperationException: Handle is not initialized.
   at System.Runtime.InteropServices.GCHandle.FromIntPtr(IntPtr value)
   at Il2CppInterop.Runtime.Runtime.ClassInjectorBase.GetMonoObjectFromIl2CppPointer(IntPtr pointer) in /home/runner/work/Il2CppInterop/Il2CppInterop/Il2CppInterop.Runtime/Runtime/ClassInjectorBase.cs:line 15
   at (il2cpp delegate trampoline) System.Void_System.Action(IntPtr , Il2CppMethodInfo* )

I could just be doing something wrong, but I've tried a bunch of variations and couldn't get it to work.

(I wasn't 100% sure if I should open here or in BepInEx/BepInEx so feel free to transfer)

PieKing1215 avatar Jan 23 '23 23:01 PieKing1215

Is this still an issue?

ds5678 avatar Jul 14 '24 16:07 ds5678