0

I am using NAudio to play Mp3 files in my .NET 4 app. First of all I initialize WaveOut:

IWavePlayer^ waveOutDevice = gcnew WaveOut();

Then I have 2 buttons. Play button :(code)

volumeStream = gcnew WaveChannel32(gcnew Mp3FileReader(gcnew IO::FileStream(path, IO::FileMode::Open, IO::FileAccess::Read, IO::FileShare::ReadWrite)));
mainOutputStream = volumeStream;
waveOutDevice->Init(mainOutputStream);
waveOutDevice->Play();

It loads MP3 form FileStream and plays it. 2nd button is Stop :(code)

waveOutDevice->Stop();

It just stops playing.

When I start my app it eats 5.344 KB.

s1

But when i hit 2 buttons (Play then Stop) (imagine i'm playing different MP3's) about 10 times app eats 14.912 KB!

s2

And I don't know how to release this memory. To play MP3 I am using these NAudio objects:

IWavePlayer^ waveOutDevice;
WaveStream^ mainOutputStream;
WaveChannel32^ volumeStream;
iamnp
  • 510
  • 8
  • 23

1 Answers1

1

As a rule, you should make sure you Dispose any .NET objects that implement IDisposable. In particular, you are not calling Dispose on your Mp3FileReader, which will not only leave the file open, but will not clean up the ACM handles it opens.

The other thing you need to be aware of as a C++ programmer using .NET objects is that .NET uses garbage collection, so the memory usage will not always immediately go down after you have finished using something. Instead, managed objects become available for garbage collection and their memory is only released once the garbage collector has run.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194