diff --git a/src/Audio/AudioDevice.cs b/src/Audio/AudioDevice.cs index 2edfe3e4..e5a86b0a 100644 --- a/src/Audio/AudioDevice.cs +++ b/src/Audio/AudioDevice.cs @@ -42,6 +42,7 @@ namespace MoonWorks.Audio private AutoResetEvent WakeSignal; internal readonly object StateLock = new object(); + private bool Running; private bool IsDisposed; public unsafe AudioDevice() @@ -130,13 +131,15 @@ namespace MoonWorks.Audio Thread.IsBackground = true; Thread.Start(); + Running = true; + TickStopwatch.Start(); previousTickTime = 0; } private void ThreadMain() { - while (!IsDisposed) + while (Running) { lock (StateLock) { @@ -278,27 +281,27 @@ namespace MoonWorks.Audio { if (!IsDisposed) { - lock (StateLock) + Running = false; + Thread.Join(); + + if (disposing) { - if (disposing) + foreach (var weakReference in resources) { - foreach (var weakReference in resources) + var target = weakReference.Target; + + if (target != null) { - var target = weakReference.Target; - - if (target != null) - { - (target as IDisposable).Dispose(); - } + (target as IDisposable).Dispose(); } - resources.Clear(); } - - FAudio.FAudioVoice_DestroyVoice(MasteringVoice); - FAudio.FAudio_Release(Handle); - - IsDisposed = true; + resources.Clear(); } + + FAudio.FAudioVoice_DestroyVoice(MasteringVoice); + FAudio.FAudio_Release(Handle); + + IsDisposed = true; } } diff --git a/src/Audio/StaticSound.cs b/src/Audio/StaticSound.cs index 975af1e8..b5def255 100644 --- a/src/Audio/StaticSound.cs +++ b/src/Audio/StaticSound.cs @@ -311,6 +311,18 @@ namespace MoonWorks.Audio protected override unsafe void Destroy() { + foreach (var instance in UsedInstances) + { + instance.Free(); + } + + foreach (var instance in AvailableInstances) + { + instance.Dispose(); + } + + AvailableInstances.Clear(); + if (OwnsBuffer) { NativeMemory.Free((void*) Handle.pAudioData); diff --git a/src/Game.cs b/src/Game.cs index 7193bfe7..183ddea7 100644 --- a/src/Game.cs +++ b/src/Game.cs @@ -86,12 +86,21 @@ namespace MoonWorks Tick(); } + Logger.LogInfo("Starting shutdown sequence..."); + + Logger.LogInfo("Cleaning up game..."); Destroy(); + Logger.LogInfo("Closing audio thread..."); AudioDevice.Dispose(); + Logger.LogInfo("Unclaiming window..."); GraphicsDevice.UnclaimWindow(MainWindow); + + Logger.LogInfo("Disposing window..."); MainWindow.Dispose(); + + Logger.LogInfo("Disposing graphics device..."); GraphicsDevice.Dispose(); SDL.SDL_Quit();