What I want to do is to Load()
a sound effect in XNA using the Content Manager and automatically create a instance to control playback. When the sound is no longer needed, I was wondering how to properly Unload()
it from memory?
Furthermore, I was wondering if Unload()
is even needed. When I call Load()
twice, does the second call properly free the memory of the first call? I would guess that the C# garbage collector automatically disposes the old effect and instance as they are being overwritten by the second call. Is this correct?
These are the parameters in my custom MySoundEffect
class:
// Has sound effect been loaded?
bool loaded;
// Store our SoundEffect resource
SoundEffect effect;
// Instance of our SoundEffect to control playback
SoundEffectInstance instance;
This method is loading the sound.
public void Load(String location)
{
effect = Content.Load<SoundEffect>(location);
if (effect != null) loaded = true;
else
{
Error.Happened("Loading of sound effect " + location + " failed.");
return;
}
// Create instance
instance = effect.CreateInstance();
}
This is called when the sound is no longer needed:
public void Unload()
{
loaded = false;
instance.Dispose();
effect.Dispose();
}