From a0c57c7a5975edbd938c2f67791b0bd49575ab96 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Tue, 22 Feb 2022 16:44:39 -0800 Subject: [PATCH] Cleanup fixes --- src/Audio/AudioDevice.cs | 15 +++++++++------ src/Game.cs | 9 +++++++++ src/Window/OSWindow.cs | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/Audio/AudioDevice.cs b/src/Audio/AudioDevice.cs index 7b792b16..ebff250b 100644 --- a/src/Audio/AudioDevice.cs +++ b/src/Audio/AudioDevice.cs @@ -240,17 +240,20 @@ namespace MoonWorks.Audio { if (disposing) { - // TODO: dispose managed state (managed objects) - foreach (var weakReference in streamingSounds) - { - if (weakReference.TryGetTarget(out var streamingSound)) + for (var i = streamingSounds.Count - 1; i >= 0; i--) + { + var weakReference = streamingSounds[i]; + + if (weakReference.TryGetTarget(out var streamingSound)) { streamingSound.Dispose(); } - } - streamingSounds.Clear(); + } + streamingSounds.Clear(); } + FAudio.FAudioVoice_DestroyVoice(ReverbVoice); + FAudio.FAudioVoice_DestroyVoice(MasteringVoice); FAudio.FAudio_Release(Handle); IsDisposed = true; diff --git a/src/Game.cs b/src/Game.cs index 9ade9559..c63097ac 100644 --- a/src/Game.cs +++ b/src/Game.cs @@ -139,7 +139,13 @@ namespace MoonWorks GraphicsDevice.SubmitDestroyCommandBuffer(); } + OnDestroy(); + + AudioDevice.Dispose(); GraphicsDevice.Dispose(); + Window.Dispose(); + + SDL.SDL_Quit(); } private void HandleSDLEvents() @@ -168,6 +174,9 @@ namespace MoonWorks // alpha refers to a percentage value between the current and next state protected abstract void Draw(TimeSpan dt, double alpha); + // Clean up any objects you created in this function + protected abstract void OnDestroy(); + private void HandleTextInput(SDL2.SDL.SDL_Event evt) { // Based on the SDL2# LPUtf8StrMarshaler diff --git a/src/Window/OSWindow.cs b/src/Window/OSWindow.cs index 4db80654..70381226 100644 --- a/src/Window/OSWindow.cs +++ b/src/Window/OSWindow.cs @@ -3,11 +3,13 @@ using SDL2; namespace MoonWorks.Window { - public class OSWindow + public class OSWindow : IDisposable { - internal IntPtr Handle { get; } + internal IntPtr Handle { get; } public ScreenMode ScreenMode { get; } + private bool IsDisposed; + public OSWindow(WindowCreateInfo windowCreateInfo) { var windowFlags = SDL.SDL_WindowFlags.SDL_WINDOW_VULKAN; @@ -59,5 +61,33 @@ namespace MoonWorks.Window { SDL.SDL_SetWindowSize(Handle, (int)width, (int)height); } - } + + protected virtual void Dispose(bool disposing) + { + if (!IsDisposed) + { + if (disposing) + { + // dispose managed state (managed objects) + } + + SDL.SDL_DestroyWindow(Handle); + + IsDisposed = true; + } + } + + ~OSWindow() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: false); + } + + public void Dispose() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + } }