Memory Leak Blues (PlayCue)

Hi

Well, my latest game is really starting to come together. However, I've noticed something that I think is the cause of a memory leak.

ParentGame.SoundBank.PlayCue("spark_1");

I've notice a positive allocation of memory whenever the "PlayCue" method on a SoundBank is called. This particular line of code is fired very regularly (its an electric arc sound) in my game.

I've been pretty religious about pre-allocating objects (and avoiding "new") in the game - To avoid unwanted garbage collection and break-up of game smoothness. Anyway. Memory allocation appears relatively stable until PlayCue is called.

Can anyone offer an advice - Is this a known issue Anyone got a way around this




Answer this question

Memory Leak Blues (PlayCue)

  • medel

    George Clingerman wrote:
    You have to be careful with that second method. If the object goes out of scope, garbage collection can come along and cut out your sound before it's finished playing. So for example, if you have a method PlaySoundFX that you pass in the type of sound effect you want to play.

    Yes. The garbage collection is cutting off the sound. Back to PlayCue() it is then

    What does audioEngine.Update() do



  • chrisb2

    You have to be careful with that second method. If the object goes out of scope, garbage collection can come along and cut out your sound before it's finished playing. So for example, if you have a method PlaySoundFX that you pass in the type of sound effect you want to play. There is a good chance your sound effect will play, then get cut off randomly when garbage collection happens (assuming that above code was in that method).

    Using the PlayCue method is the recommended way to play sound effects. You should only create a cue object if you have a need to start, stop and/or pause a sound in your game (background music for example).

    As far as the memory issues, are you calling audioEngine.Update() in your Update code That should help you with your leak.


  • foxster100

    Nice idea. I tried it out. Unfortunately, once a Cue has been played, if you want to play the Cue again, it must be re-acquired (GetCue).

    See ms-help://MS.VSExpressCC.v80/MS.VSIPCC.v80/MS.XNAFX.1033/XNA/Audio_HowTo_StopOrPauseASound.htm..

    That said, using GetCue followed by an immediate Play() appears a lot less hungry on memory than PlayCue().

    Unless anyone has any other observations, I would advise using the code...

    Cue c = SoundBank.GetCue("foobar");
    c.Play();

    ...And avoid SoundBank.PlayCue("foobar");

    Its not perfect, but if you are firing a lot of sounds off, it certainly appears less leaky. Thanks for the suggestion Leaf



  • Santhosh Pallikara

    Basically, audioEngine.Update tells the audio engine to go ahead and do some clean up work and other maintenance work required by the audio engine. You should run that about once per frame (so sticking it in the update portion of your code is a great place to put it).


  • Sveind

    I've not tried this but it might help. You could use something like this instead:

    // call this once per cue
    Cue spark = Soundbank.GetCue("spark_1");

    // then each time you want to play the sound
    spark.Play();

    In other words, cache the cues that you are going to use.

    Cheers,
    Leaf.


  • Memory Leak Blues (PlayCue)