From d123dd3eb462621ae330ebe8f24b3f17a5588c0f Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 18 Mar 2025 17:42:21 +0100 Subject: [PATCH] Use limited attempts for assembly context reload and dont use Debug Log for warning #2469 --- Source/Engine/Engine/NativeInterop.Unmanaged.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Source/Engine/Engine/NativeInterop.Unmanaged.cs b/Source/Engine/Engine/NativeInterop.Unmanaged.cs index ffbc99b6f..5a437dad2 100644 --- a/Source/Engine/Engine/NativeInterop.Unmanaged.cs +++ b/Source/Engine/Engine/NativeInterop.Unmanaged.cs @@ -1062,7 +1062,7 @@ namespace FlaxEngine.Interop // Wait for previous ALC to finish unloading, track it without holding strong references to it GCHandle weakRef = GCHandle.Alloc(scriptingAssemblyLoadContext, GCHandleType.WeakTrackResurrection); scriptingAssemblyLoadContext = null; -#if true +#if false // In case the ALC doesn't unload properly: https://learn.microsoft.com/en-us/dotnet/standard/assembly/unloadability#debug-unloading-issues while (true) #else @@ -1077,7 +1077,7 @@ namespace FlaxEngine.Interop System.Threading.Thread.Sleep(1); } if (IsHandleAlive(weakRef)) - Debug.LogWarning("Scripting AssemblyLoadContext was not unloaded."); + Debug.Logger.LogHandler.LogWrite(LogType.Warning, "Scripting AssemblyLoadContext was not unloaded."); weakRef.Free(); static bool IsHandleAlive(GCHandle weakRef) @@ -1142,8 +1142,8 @@ namespace FlaxEngine.Interop { // HACK: Workaround for TypeDescriptor holding references to collectible types (https://github.com/dotnet/runtime/issues/30656) - Type TypeDescriptionProviderType = typeof(System.ComponentModel.TypeDescriptionProvider); - MethodInfo clearCacheMethod = TypeDescriptionProviderType?.Assembly.GetType("System.ComponentModel.ReflectionCachesUpdateHandler")?.GetMethod("ClearCache"); + Type typeDescriptionProviderType = typeof(System.ComponentModel.TypeDescriptionProvider); + MethodInfo clearCacheMethod = typeDescriptionProviderType?.Assembly.GetType("System.ComponentModel.ReflectionCachesUpdateHandler")?.GetMethod("ClearCache"); if (clearCacheMethod != null) clearCacheMethod.Invoke(null, new object[] { null }); @@ -1183,9 +1183,8 @@ namespace FlaxEngine.Interop static void InvokeClear(object instance) { Type type = instance.GetType(); - FlaxEngine.Assertions.Assert.IsTrue(type.Name == "ConcurrentDictionary`2" || type.Name == "Hashtable" || type.Name == "WeakHashtable"); - type.GetMethods(BindingFlags.Instance | BindingFlags.Public).FirstOrDefault(x => x.Name == "Clear") - ?.Invoke(instance, new object[] { }); + Assertions.Assert.IsTrue(type.Name == "ConcurrentDictionary`2" || type.Name == "Hashtable" || type.Name == "WeakHashtable"); + type.GetMethods(BindingFlags.Instance | BindingFlags.Public).FirstOrDefault(x => x.Name == "Clear")?.Invoke(instance, Array.Empty()); } }