Now that I have identified the problem, I thought about various ways to resolve it. But I really don't know what the best method is.
So I'm seeking advice here
The problem(as already said) is that sounds play too long and therefore get promoted to higher generations where they are not disposed in time to make place for new sound instances.
Thats why the internal Sound counter in SFML rises and doesn't decrease (fast enough) even though no more sounds are played.
Method 1:
Run the GC more often and on all generations.
But it takes time and introduces a bug and unnecessary overhead!
Method 2: (what I'm currently doing)
I have a static class that accepts "IDisposable" objects and a time.
When the time is up the class calls .Dispose(); on the object to free its resources (hence decreasing the sound counter).
The class has it's own low-priority background thread to carry out its task.
So everytime I need a explosion sound I do the following:
Sound s = new Sound(...);
ObjectDestroyer.RegisterDestruction(s, s.SoundBuffer.Duration);
Is there a better way that I'm overlooking?
I don't like the idea to have just another background thread besides the GC that only collects garbage...
Isn't there a "event" like OnSoundFinishedPlaying that I can register for?
How would you handle this situation Laurent ?