Skip to content

Commit

Permalink
Decouple ProcessExit handlers for SoundEffect/AudioEngine/MediaPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
flibitijibibo committed Jan 3, 2025
1 parent d90a2ca commit 85a8457
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 31 deletions.
18 changes: 18 additions & 0 deletions src/Audio/AudioEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ ref notificationDesc

#endregion

#region Static Constructor

static AudioEngine()
{
AppDomain.CurrentDomain.ProcessExit += ProgramExit;
}

#endregion

#region Destructor

~AudioEngine()
Expand Down Expand Up @@ -423,5 +432,14 @@ private static unsafe void OnXACTNotification(IntPtr notification)
}

#endregion

#region Internal Static Methods

internal static void ProgramExit(object sender, EventArgs e)
{
ProgramExiting = true;
}

#endregion
}
}
17 changes: 16 additions & 1 deletion src/Audio/SoundEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ int loopLength

~SoundEffect()
{
if (Instances.Count > 0)
if (!FAudioContext.ProgramExiting && Instances.Count > 0)
{
// STOP LEAKING YOUR INSTANCES, ARGH
GC.ReRegisterForFinalize(this);
Expand Down Expand Up @@ -534,6 +534,7 @@ public static SoundEffect FromStream(Stream stream)
internal class FAudioContext
{
public static FAudioContext Context = null;
public static bool ProgramExiting = false;

public readonly IntPtr Handle;
public readonly byte[] Handle3D;
Expand Down Expand Up @@ -758,6 +759,20 @@ out devices
}

Context = context;

AppDomain.CurrentDomain.ProcessExit += ProgramExit;
}

private static void ProgramExit(object sender, EventArgs e)
{
ProgramExiting = true;

if (Context != null)
{
GC.Collect(); // Desperate last bid to collect SoundEffectInstances

SoundEffect.FAudioContext.Context.Dispose();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Audio/SoundEffectInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ internal SoundEffectInstance(SoundEffect parent = null)

~SoundEffectInstance()
{
if (!IsDisposed && State == SoundState.Playing)
if (!SoundEffect.FAudioContext.ProgramExiting && !IsDisposed && State == SoundState.Playing)
{
// STOP LEAKING YOUR INSTANCES, ARGH
GC.ReRegisterForFinalize(this);
Expand Down
10 changes: 0 additions & 10 deletions src/FNAPlatform/SDL2_FNAPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,6 @@ out prevUserData

public static void ProgramExit(object sender, EventArgs e)
{
AudioEngine.ProgramExiting = true;

if (SoundEffect.FAudioContext.Context != null)
{
GC.Collect(); // Desperate last bid to collect SoundEffectInstances

SoundEffect.FAudioContext.Context.Dispose();
}
Media.MediaPlayer.DisposeIfNecessary();

// This _should_ be the last SDL call we make...
SDL.SDL_QuitSubSystem(
SDL.SDL_INIT_VIDEO |
Expand Down
10 changes: 0 additions & 10 deletions src/FNAPlatform/SDL3_FNAPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,16 +280,6 @@ out prevUserData

public static void ProgramExit(object sender, EventArgs e)
{
AudioEngine.ProgramExiting = true;

if (SoundEffect.FAudioContext.Context != null)
{
GC.Collect(); // Desperate last bid to collect SoundEffectInstances

SoundEffect.FAudioContext.Context.Dispose();
}
Media.MediaPlayer.DisposeIfNecessary();

// This _should_ be the last SDL call we make...
SDL.SDL_QuitSubSystem(
SDL.SDL_InitFlags.SDL_INIT_VIDEO |
Expand Down
19 changes: 10 additions & 9 deletions src/Media/MediaPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public static bool IsVisualizationEnabled
static MediaPlayer()
{
Queue = new MediaQueue();
AppDomain.CurrentDomain.ProcessExit += ProgramExit;
}

#endregion
Expand Down Expand Up @@ -305,15 +306,6 @@ internal static void Update()
MoveNext();
}

internal static void DisposeIfNecessary()
{
if (initialized)
{
FAudio.XNA_SongQuit();
initialized = false;
}
}

internal static void OnActiveSongChanged()
{
if (ActiveSongChanged != null)
Expand Down Expand Up @@ -392,6 +384,15 @@ private static void PlaySong(Song song)
State = MediaState.Playing;
}

private static void ProgramExit(object sender, EventArgs e)
{
if (initialized)
{
FAudio.XNA_SongQuit();
initialized = false;
}
}

#endregion

}
Expand Down

0 comments on commit 85a8457

Please sign in to comment.