Fix Sound memory leak (OutOfMemoryException), remove Music caching to free memory
This commit is contained in:
@@ -39,6 +39,8 @@ namespace OpenRA
|
||||
readonly ISoundEngine soundEngine;
|
||||
Cache<string, ISoundSource> sounds;
|
||||
ISoundSource rawSource;
|
||||
Func<string, ISoundSource> getSoundSource;
|
||||
ISoundSource musicSource;
|
||||
ISound music;
|
||||
ISound video;
|
||||
MusicInfo currentMusic;
|
||||
@@ -81,9 +83,18 @@ namespace OpenRA
|
||||
|
||||
public void Initialize(ISoundLoader[] loaders, IReadOnlyFileSystem fileSystem)
|
||||
{
|
||||
sounds = new Cache<string, ISoundSource>(s => LoadSound(loaders, fileSystem, s));
|
||||
StopMusic();
|
||||
soundEngine.ReleaseSourcePool();
|
||||
|
||||
if (sounds != null)
|
||||
foreach (var soundSource in sounds.Values)
|
||||
soundSource.Dispose();
|
||||
|
||||
getSoundSource = s => LoadSound(loaders, fileSystem, s);
|
||||
sounds = new Cache<string, ISoundSource>(getSoundSource);
|
||||
currentSounds = new Dictionary<uint, ISound>();
|
||||
music = null;
|
||||
musicSource = null;
|
||||
currentMusic = null;
|
||||
video = null;
|
||||
}
|
||||
@@ -195,11 +206,11 @@ namespace OpenRA
|
||||
|
||||
StopMusic();
|
||||
|
||||
var sound = sounds[m.Filename];
|
||||
if (sound == null)
|
||||
musicSource = getSoundSource(m.Filename);
|
||||
if (musicSource == null)
|
||||
return;
|
||||
|
||||
music = soundEngine.Play2D(sound, false, true, WPos.Zero, MusicVolume, false);
|
||||
music = soundEngine.Play2D(musicSource, false, true, WPos.Zero, MusicVolume, false);
|
||||
currentMusic = m;
|
||||
MusicPlaying = true;
|
||||
}
|
||||
@@ -222,7 +233,13 @@ namespace OpenRA
|
||||
public void StopMusic()
|
||||
{
|
||||
if (music != null)
|
||||
{
|
||||
soundEngine.StopSound(music);
|
||||
soundEngine.ReleaseSound(music);
|
||||
music = null;
|
||||
musicSource.Dispose();
|
||||
musicSource = null;
|
||||
}
|
||||
|
||||
MusicPlaying = false;
|
||||
currentMusic = null;
|
||||
|
||||
@@ -25,6 +25,8 @@ namespace OpenRA
|
||||
void StopAllSounds();
|
||||
void SetListenerPosition(WPos position);
|
||||
void SetSoundVolume(float volume, ISound music, ISound video);
|
||||
void ReleaseSourcePool();
|
||||
void ReleaseSound(ISound sound);
|
||||
}
|
||||
|
||||
public class SoundDevice
|
||||
@@ -39,7 +41,7 @@ namespace OpenRA
|
||||
}
|
||||
}
|
||||
|
||||
public interface ISoundSource { }
|
||||
public interface ISoundSource : IDisposable { }
|
||||
|
||||
public interface ISound
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user