This project has moved. For the latest updates, please go here.

Memory Leak on continue play audio stream using DirectSound

Aug 11, 2011 at 9:08 AM

Please Help,
    I'm using code below to play mp3 audio stream that get from database and play it's continuously
It's look like good but I found the problem about memory leak, which the memory increased steadily.
Has anyone ever encountered a problem like this and how to solve this problem?

public void GetAudioFromByteArray(byte[] AudioByteArrayTemp)
{
    DisposeWave();
    MemoryStream ms = new MemoryStream(AudioByteArrayTemp);
    WaveStream mp3Reader = new Mp3FileReader(ms);
    WaveStream pcm =    WaveFormatConversionStream.CreatePcmStream(mp3Reader);
    blockAlignedStream = new BlockAlignReductionStream(pcm);
    SoundOut.OutputSound.Init(blockAlignedStream);
    SoundOut.OutputSound.PlaybackStopped += new EventHandler(waveOutDevice_PlaybackStopped);
    SoundOut.OutputSound.Play();
}

private void DisposeWave()
{
     if (SoundOut.OutputSound != null)
     {
         if (SoundOut.OutputSound.PlaybackState == PlaybackState.Playing)
         {
             SoundOut.OutputSound.PlaybackStopped -= waveOutDevice_PlaybackStopped;
         }
         SoundOut.OutputSound.Stop();
         SoundOut.OutputSound.Dispose();
         SoundOut.OutputSound = null;
     }
     if (blockAlignedStream != null)
     {
          blockAlignedStream.Dispose();
          blockAlignedStream = null;
     }
}

Coordinator
Aug 11, 2011 at 9:45 AM

There's no obvious memory leak there. It possibly depends on what your SoundOut class is doing. Also, is it really a memory leak? You know that .NET doesn't free up memory immediately, but only when the garbage collector runs, right?

Aug 11, 2011 at 10:22 AM

Thank you Mark for your reply.

This is my SoundOut class it's just Singleton.

// Singleton
public sealed class SoundOut
{
    static DirectSoundOut outputSound = null;
    static readonly object padlock = new object();

    SoundOut() { }

    public static DirectSoundOut OutputSound
    {
        get 
        {
            lock (padlock)
            {
                if (outputSound == null) 
                { 
                    outputSound = new DirectSoundOut();
                }
                    return outputSound;
            }
        }
            set { outputSound = value; }
    }
}
I try to force Garbage Collection [GC.Collect()] for solve this problem but it's not decrease.
I'm notice at "GetAudioFromByteArray()" it's not return memory after called.
please suggest me.
Thank you very much



Coordinator
Aug 11, 2011 at 10:23 AM

I'd suggest you use a memory profiler and see what is being held onto.