Several changes to improve unloadability of assemblies.

This commit is contained in:
MapleWheels
2026-04-29 10:20:16 -04:00
parent 3f199a9500
commit 66717c7fe7
5 changed files with 17 additions and 10 deletions

View File

@@ -558,7 +558,8 @@ public sealed class AssemblyLoader : AssemblyLoadContext, IAssemblyLoaderService
IsDisposed = true;
this.Unload();
this.DisposeInternal();
GC.SuppressFinalize(this);
// we want to call base finalizers
//GC.SuppressFinalize(this);
}
~AssemblyLoader()
@@ -579,6 +580,7 @@ public sealed class AssemblyLoader : AssemblyLoadContext, IAssemblyLoaderService
}
var wf = new WeakReference<IAssemblyLoaderService>(this);
_loadedAssemblyData.Clear();
_onUnload?.Invoke(this);
}

View File

@@ -23,6 +23,7 @@ public sealed partial class ModConfigFileParserService :
public ModConfigFileParserService(IStorageService storageService)
{
_storageService = storageService;
_storageService.UseCaching = false;
}
#region Dispose

View File

@@ -45,6 +45,7 @@ public sealed class ModConfigService : IModConfigService
#if CLIENT
_stylesParserService = stylesParserService;
#endif
_storageService.UseCaching = false;
}
#region Dispose

View File

@@ -174,6 +174,7 @@ public class PluginManagementService : IAssemblyManagementService
_pluginInjectorContainer?.Dispose();
_pluginInjectorContainer = null;
ReflectionUtils.ResetCache();
foreach (var loader in _assemblyLoaders)
{
try
@@ -184,14 +185,6 @@ public class PluginManagementService : IAssemblyManagementService
catch (Exception e)
{
_logger?.LogError($"Failed to dispose of {nameof(IAssemblyLoaderService)} for ContentPackage {loader.Key.Name}: \n{e.Message}");
if (loader.Value.Assemblies.Any())
{
foreach (var ass in loader.Value.Assemblies)
{
_logger?.LogWarning($"{nameof(PluginManagementService)}: Fallback manual unsubscription of assemblies: {ass.GetName()}");
ReflectionUtils.RemoveAssemblyFromCache(ass);
}
}
}
}
_assemblyLoaders.Clear();
@@ -837,7 +830,9 @@ public class PluginManagementService : IAssemblyManagementService
_assemblyLoaders.Clear();
_storageService.PurgeCache();
GC.Collect();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true);
GC.WaitForFullGCComplete(1000);
#if DEBUG
// Print still loaded assembly load ctx after giving some time
@@ -917,6 +912,14 @@ public class PluginManagementService : IAssemblyManagementService
_eventService.Value.RemoveDispatcherEventService(_pluginEventService);
_pluginEventService = null;
}
try
{
_pluginInjectorContainer.Dispose();
}
catch (Exception e)
{
results.WithError(new ExceptionalError(e));
}
_pluginInjectorContainer = null;
_pluginInstances.Clear();

View File

@@ -114,8 +114,8 @@ namespace Barotrauma
public static void ResetCache()
{
CachedNonAbstractTypes.Clear();
CachedNonAbstractTypes.TryAdd(typeof(ReflectionUtils).Assembly, typeof(ReflectionUtils).Assembly.GetTypes().Where(t => !t.IsAbstract).ToImmutableArray());
TypeSearchCache.Clear();
CachedNonAbstractTypes.TryAdd(typeof(ReflectionUtils).Assembly, typeof(ReflectionUtils).Assembly.GetTypes().Where(t => !t.IsAbstract).ToImmutableArray());
}
public static Type? GetType(string nameWithNamespace)