From ff56152ef2b9166345405e93ba1fa4de6a1c2b11 Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sat, 15 Jul 2023 13:19:22 +0300 Subject: [PATCH] Fix releasing non-collectible types with collectible generic types --- .../Engine/Engine/NativeInterop.Unmanaged.cs | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Source/Engine/Engine/NativeInterop.Unmanaged.cs b/Source/Engine/Engine/NativeInterop.Unmanaged.cs index 17b8b73af..57454ad1e 100644 --- a/Source/Engine/Engine/NativeInterop.Unmanaged.cs +++ b/Source/Engine/Engine/NativeInterop.Unmanaged.cs @@ -244,7 +244,25 @@ namespace FlaxEngine.Interop @namespace = NativeAllocStringAnsi(type.Namespace ?? ""), typeAttributes = (uint)type.Attributes, }; - *assemblyHandle = GetAssemblyHandle(type.Assembly); + + Assembly assembly = null; + if (type.IsGenericType && !type.Assembly.IsCollectible) + { + // The owning assembly of a generic type with type arguments referencing + // collectible assemblies must be one of the collectible assemblies. + foreach (var genericType in type.GetGenericArguments()) + { + if (genericType.Assembly.IsCollectible) + { + assembly = genericType.Assembly; + break; + } + } + } + if (assembly == null) + assembly = type.Assembly; + + *assemblyHandle = GetAssemblyHandle(assembly); } [UnmanagedCallersOnly]