Getting an error when exiting program after loading .wav using NAudio
I just found NAudio the other day and I've been playing with it. I have written a simple windows form program that has a load button (loads a specific .wav song), and buttons to play, pause, stop, fade the sound in (my own added functionality using timers and a gradually increasing volume), and fade the sound out. I also have a trackbar control to handle the volume. I created a wrapper class to put all the NAudio stuff in, as well as to add-on extra functionality like fade-in/out and event notifications when a sound starts or is paused.
Okay, all said, that works fine. The program correctly loads, plays, pauses, stops, and fades in and out. The volume trackbar correctly reflects the volume level of the song being played. That much works, but I do have two problems.
First, after I click the "Load" button to load the sound in and then do all the playing I want to (or not), when I exit the program I get the following popup error message ("Assertion failed"): "AcmStreamHeader dispose was not called at AcmStreamHeader.Finalize()". This is one of those "Abort, Retry, Ignore" popups, but after a few seconds it disappears and the project terminates. (Note: The load functionality creates the DirectSoundOut and calls CreateInputStream... taken directly from the NAudio samples on the site. It also sets up event handlers for the defined events, but that probably doesn't matter.)
In the class destructor, I make the following calls:
mainOutputStream.Close();
mainOutputStream.Dispose();
waveOutDevice.Dispose();
But I still get the error. This is the big one, and I'll ask the other question in another thread. Any idea why this is i开发者_如何学Gos happening and how I can stop it?
I am running VS 10 on Windows 7 32-bit.
The most likely reason for the error (it's actually just a Debug.Assert - it doesn't occur in release builds of NAudio), is mainOutputStream was assigned to more than one WaveStream and when you changed it, you didn't Dispose the old one. When you exit the app, the Garbage Collector runs and this Debug.Assert fires.
A couple of other points:
- There is no need to call mainOutputStream.Close and Dispose. Just call one of them.
- Your cleanup code should not be in a class destructor (finalizer?), but in a Dispose method, for a Form, in the Closing or Closed event handler.
- Edit: Another possible reason is that you derived from WaveStream and in your overriden Dispose method didn't call Dispose on any WaveStreams that are members of class.
精彩评论