Dispose of disposable ApiHawk libs when reinitialising

This commit is contained in:
YoshiRulz 2021-01-19 01:12:50 +10:00
parent 5c89d0cb15
commit bba4286123
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
2 changed files with 18 additions and 6 deletions

View File

@ -5,7 +5,7 @@ using System.Collections.Generic;
namespace BizHawk.Client.Common
{
public sealed class ApiContainer
public sealed class ApiContainer : IDisposable
{
public readonly IReadOnlyDictionary<Type, IExternalApi> Libraries;
@ -26,5 +26,10 @@ namespace BizHawk.Client.Common
public IToolApi Tool => (IToolApi) Libraries[typeof(IToolApi)];
public ApiContainer(IReadOnlyDictionary<Type, IExternalApi> libs) => Libraries = libs;
public void Dispose()
{
foreach (var lib in Libraries.Values) if (lib is IDisposable disposableLib) disposableLib.Dispose();
}
}
}

View File

@ -28,7 +28,6 @@ namespace BizHawk.Client.EmuHawk
_apiTypes = list.ToArray();
}
/// <remarks>TODO do we need to keep references to these because of GC weirdness? --yoshi</remarks>
private static ApiContainer? _container;
private static ApiContainer? _luaContainer;
@ -78,8 +77,12 @@ namespace BizHawk.Client.EmuHawk
ToolManager toolManager,
Config config,
IEmulator emulator,
IGameInfo game
) => new BasicApiProvider(_container = Register( serviceProvider, Console.WriteLine, mainForm, displayManager, inputManager, movieSession, toolManager, config, emulator, game));
IGameInfo game)
{
_container?.Dispose();
_container = Register(serviceProvider, Console.WriteLine, mainForm, displayManager, inputManager, movieSession, toolManager, config, emulator, game);
return new BasicApiProvider(_container);
}
public static ApiContainer RestartLua(
IEmulatorServiceProvider serviceProvider,
@ -91,7 +94,11 @@ namespace BizHawk.Client.EmuHawk
ToolManager toolManager,
Config config,
IEmulator emulator,
IGameInfo game
) => _luaContainer = Register(serviceProvider, logCallback, mainForm, displayManager, inputManager, movieSession, toolManager, config, emulator, game);
IGameInfo game)
{
_luaContainer?.Dispose();
_luaContainer = Register(serviceProvider, logCallback, mainForm, displayManager, inputManager, movieSession, toolManager, config, emulator, game);
return _luaContainer;
}
}
}