From bba42861230927d624ebd7f043ff9a34a46de145 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Tue, 19 Jan 2021 01:12:50 +1000 Subject: [PATCH] Dispose of disposable ApiHawk libs when reinitialising --- src/BizHawk.Client.Common/Api/ApiContainer.cs | 7 ++++++- src/BizHawk.Client.EmuHawk/Api/ApiManager.cs | 17 ++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/BizHawk.Client.Common/Api/ApiContainer.cs b/src/BizHawk.Client.Common/Api/ApiContainer.cs index 281e639071..14f3cd7f8f 100644 --- a/src/BizHawk.Client.Common/Api/ApiContainer.cs +++ b/src/BizHawk.Client.Common/Api/ApiContainer.cs @@ -5,7 +5,7 @@ using System.Collections.Generic; namespace BizHawk.Client.Common { - public sealed class ApiContainer + public sealed class ApiContainer : IDisposable { public readonly IReadOnlyDictionary Libraries; @@ -26,5 +26,10 @@ namespace BizHawk.Client.Common public IToolApi Tool => (IToolApi) Libraries[typeof(IToolApi)]; public ApiContainer(IReadOnlyDictionary libs) => Libraries = libs; + + public void Dispose() + { + foreach (var lib in Libraries.Values) if (lib is IDisposable disposableLib) disposableLib.Dispose(); + } } } diff --git a/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs b/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs index c11cea8866..12efc8e0a6 100644 --- a/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs +++ b/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs @@ -28,7 +28,6 @@ namespace BizHawk.Client.EmuHawk _apiTypes = list.ToArray(); } - /// TODO do we need to keep references to these because of GC weirdness? --yoshi 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; + } } }