diff --git a/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs b/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs index ef7c40e4..8c73caaf 100644 --- a/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs +++ b/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs @@ -82,10 +82,10 @@ public T Unbox() where T : unmanaged } private static readonly Type[] _intPtrTypeArray = { typeof(IntPtr) }; - private static readonly MethodInfo _getUninitializedObject = typeof(RuntimeHelpers).GetMethod(nameof(RuntimeHelpers.GetUninitializedObject))!; + private static readonly MethodInfo _getUninitializedObject = typeof(RuntimeHelpers).GetMethod(nameof(FormatterServices.GetUninitializedObject))!; private static readonly MethodInfo _getTypeFromHandle = typeof(Type).GetMethod(nameof(Type.GetTypeFromHandle))!; - private static readonly MethodInfo _createGCHandle = typeof(Il2CppObjectBase).GetMethod(nameof(CreateGCHandle))!; - private static readonly FieldInfo _isWrapped = typeof(Il2CppObjectBase).GetField(nameof(isWrapped))!; + private static readonly MethodInfo _createGCHandle = typeof(Il2CppObjectBase).GetMethod(nameof(CreateGCHandle), BindingFlags.Instance | BindingFlags.NonPublic)!; + private static readonly FieldInfo _isWrapped = typeof(Il2CppObjectBase).GetField(nameof(isWrapped), BindingFlags.Instance | BindingFlags.NonPublic)!; internal static class InitializerStore { @@ -126,7 +126,7 @@ private static Func Create() // obj.isWrapped = true; il.Emit(OpCodes.Dup); il.Emit(OpCodes.Ldc_I4_1); - il.Emit(OpCodes.Stsfld, _isWrapped); + il.Emit(OpCodes.Stfld, _isWrapped); var parameterlessConstructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, Type.EmptyTypes); if (parameterlessConstructor != null) diff --git a/Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs b/Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs index bd70a8ad..54a18d9a 100644 --- a/Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs +++ b/Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs @@ -40,10 +40,14 @@ public static T Get(IntPtr ptr) var newObj = Il2CppObjectBase.InitializerStore.Initializer(ptr); unsafe { - var nativeClassStruct = UnityVersionHandler.Wrap((Il2CppClass*)Il2CppClassPointerStore.NativeClassPtr); - if (!nativeClassStruct.HasFinalize) + var il2CppClass = (Il2CppClass*)Il2CppClassPointerStore.NativeClassPtr; + if (il2CppClass != null) { - Il2CppSystem.GC.ReRegisterForFinalize(newObj as Object ?? new Object(ptr)); + var nativeClassStruct = UnityVersionHandler.Wrap(il2CppClass); + if (!nativeClassStruct.HasFinalize) + { + Il2CppSystem.GC.ReRegisterForFinalize(newObj as Object ?? new Object(ptr)); + } } }