Hi,
I am experiencing reproducible crashes when using Delegate.CreateDelegate() in IL2CPP builds in Unity 2021.3f5 LTS. I already filed a bug report (IN-3846), but did not get any response within a month. This is why I try my luck at the forums. Maybe, there is someone knowing a fix or potential workaround.
What happened
The built project crashes during the execution of method “Delegate.CreateDelegate”
How to reproduce
Environment:
- 2021.3.5f1 LTS, Windows
- Empty Unity 3D project, no specific packages installed
- Scripting Backend IL2CPP
- Default Visual Studio 2019 installation from UnityHub
Steps:
- Add “Test.cs” to any GameObject in an empty scene
using UnityEngine;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
using System;
public class Test : MonoBehaviour {
void Start() {
MethodInfo[] methodInfos = typeof(Test).GetMethods(BindingFlags.Public | BindingFlags.Static);
foreach (MethodInfo methodInfo in methodInfos) {
if (methodInfo.Name.StartsWith("Test")) {
Func<Type[], Type> getType = Expression.GetActionType;
var types = methodInfo.GetParameters().Select(p => p.ParameterType);
Delegate.CreateDelegate(getType(types.ToArray()), methodInfo);
}
}
}
public static void Test1_Ok(string s1) { }
public static void Test2_Ok(bool b1) { }
public static void Test3_Ok(string s1, bool b1) { }
public static void Test4_Crash(bool b1, string s1) { }
public static void Test5_Ok(int i1, string s1) { }
public static void Test6_Crash(int i1, bool b1) { }
}
- Build the project and run
The player crashes after showing the splash screen.
Notes:
-
The issue does not appear on Mono runtime
-
The issue does not appear on IL2CPP Code Generation: Faster (smaller) Builds
-
The issue does not appear on Unity 2020.3.34f1 LTS or Unity 2022.1.2
-
The attached Test.cs contains different “Test*”-methods that have different parameters. Depending on the set of parameters, CreateDelegate works or crashes the player.
-
The Player.log can be found in the attachment
8248518–1079211–Player.log.txt (37.5 KB)