From c7d277ca747ba78fac6ecbb20a125b583450dce3 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 29 Aug 2023 11:42:47 +0200 Subject: [PATCH] Fix crash when unboxing managed structure with refs into `Variant` --- Source/Engine/Engine/NativeInterop.Unmanaged.cs | 1 - Source/Engine/Engine/NativeInterop.cs | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Engine/NativeInterop.Unmanaged.cs b/Source/Engine/Engine/NativeInterop.Unmanaged.cs index 8837d460b..879f804d2 100644 --- a/Source/Engine/Engine/NativeInterop.Unmanaged.cs +++ b/Source/Engine/Engine/NativeInterop.Unmanaged.cs @@ -649,7 +649,6 @@ namespace FlaxEngine.Interop Type type = value.GetType(); if (!type.IsValueType) return ManagedHandle.ToIntPtr(handle); - return ValueTypeUnboxer.GetPointer(value, type); } diff --git a/Source/Engine/Engine/NativeInterop.cs b/Source/Engine/Engine/NativeInterop.cs index e75877878..dd6087b98 100644 --- a/Source/Engine/Engine/NativeInterop.cs +++ b/Source/Engine/Engine/NativeInterop.cs @@ -1077,13 +1077,14 @@ namespace FlaxEngine.Interop alloc.ptr = new IntPtr(NativeAlloc(size)); alloc.size = size; } - Unsafe.Write(alloc.ptr.ToPointer(), value); return alloc.ptr; } private static IntPtr UnboxPointer(object value, object converter) where T : struct { + if (RuntimeHelpers.IsReferenceOrContainsReferences()) // Cannot pin structure with references + return IntPtr.Zero; PinValue(value); return new IntPtr(Unsafe.AsPointer(ref Unsafe.Unbox(value))); }