fix AudioDevice crash on shutdown

pull/50/head
cosmonaut 2023-07-28 15:02:20 -07:00
parent e73c7ede55
commit 81cd397013
3 changed files with 40 additions and 16 deletions

View File

@ -42,6 +42,7 @@ namespace MoonWorks.Audio
private AutoResetEvent WakeSignal; private AutoResetEvent WakeSignal;
internal readonly object StateLock = new object(); internal readonly object StateLock = new object();
private bool Running;
private bool IsDisposed; private bool IsDisposed;
public unsafe AudioDevice() public unsafe AudioDevice()
@ -130,13 +131,15 @@ namespace MoonWorks.Audio
Thread.IsBackground = true; Thread.IsBackground = true;
Thread.Start(); Thread.Start();
Running = true;
TickStopwatch.Start(); TickStopwatch.Start();
previousTickTime = 0; previousTickTime = 0;
} }
private void ThreadMain() private void ThreadMain()
{ {
while (!IsDisposed) while (Running)
{ {
lock (StateLock) lock (StateLock)
{ {
@ -278,8 +281,9 @@ namespace MoonWorks.Audio
{ {
if (!IsDisposed) if (!IsDisposed)
{ {
lock (StateLock) Running = false;
{ Thread.Join();
if (disposing) if (disposing)
{ {
foreach (var weakReference in resources) foreach (var weakReference in resources)
@ -300,7 +304,6 @@ namespace MoonWorks.Audio
IsDisposed = true; IsDisposed = true;
} }
} }
}
~AudioDevice() ~AudioDevice()
{ {

View File

@ -311,6 +311,18 @@ namespace MoonWorks.Audio
protected override unsafe void Destroy() protected override unsafe void Destroy()
{ {
foreach (var instance in UsedInstances)
{
instance.Free();
}
foreach (var instance in AvailableInstances)
{
instance.Dispose();
}
AvailableInstances.Clear();
if (OwnsBuffer) if (OwnsBuffer)
{ {
NativeMemory.Free((void*) Handle.pAudioData); NativeMemory.Free((void*) Handle.pAudioData);

View File

@ -86,12 +86,21 @@ namespace MoonWorks
Tick(); Tick();
} }
Logger.LogInfo("Starting shutdown sequence...");
Logger.LogInfo("Cleaning up game...");
Destroy(); Destroy();
Logger.LogInfo("Closing audio thread...");
AudioDevice.Dispose(); AudioDevice.Dispose();
Logger.LogInfo("Unclaiming window...");
GraphicsDevice.UnclaimWindow(MainWindow); GraphicsDevice.UnclaimWindow(MainWindow);
Logger.LogInfo("Disposing window...");
MainWindow.Dispose(); MainWindow.Dispose();
Logger.LogInfo("Disposing graphics device...");
GraphicsDevice.Dispose(); GraphicsDevice.Dispose();
SDL.SDL_Quit(); SDL.SDL_Quit();