Several changes to improve unloadability of assemblies.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ public sealed partial class ModConfigFileParserService :
|
||||
public ModConfigFileParserService(IStorageService storageService)
|
||||
{
|
||||
_storageService = storageService;
|
||||
_storageService.UseCaching = false;
|
||||
}
|
||||
|
||||
#region Dispose
|
||||
|
||||
@@ -45,6 +45,7 @@ public sealed class ModConfigService : IModConfigService
|
||||
#if CLIENT
|
||||
_stylesParserService = stylesParserService;
|
||||
#endif
|
||||
_storageService.UseCaching = false;
|
||||
}
|
||||
|
||||
#region Dispose
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user