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

Access Violation Exception - 0xc0000005

Dec 10, 2014 at 2:16 PM
Hi Mark,

I have a problem that happens very rarely. Sometimes my application will crash after a play command. I am not able to catch the exception i my application domain. My windows logs will tell me Event Name: BEX with error code c0000005. I am using NAudio 1.7 and the directsoundout class.

Windows logs were able to report the following error prior to my c0000005 error:

Exception Info: System.AccessViolationException
Stack:
at NAudio.Wave.DirectSoundOut+IDirectSoundBuffer.Stop()
at NAudio.Wave.DirectSoundOut.StopPlayback()
at NAudio.Wave.DirectSoundOut.PlaybackThreadFunc()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()

Any ideas what can cause this AccessViolationException?
Dec 15, 2014 at 1:01 PM
I get these too actually. I have had this problem for a while but never investigated it. I did yesterday and saw the c0000005 error too, multiple times. I thought it might be something else than NAudio but it seems that it is the cause..
Dec 15, 2014 at 1:03 PM
My errors also happends randomly btw but i've only seen it on one of my PCs, running Windows 8.1. Never on my Windows 7.
Coordinator
Dec 15, 2014 at 1:21 PM
I'm afraid these types of thing can be very hard to track down. Make sure your drivers are up to date. Also consider using WaveOutEvent instead of DirectSoundOut to see if that improves matters.
Dec 15, 2014 at 6:53 PM
Yup, thats a headache..

Which drivers are you reffering to, audio drivers, motherboard drivers or?
Coordinator
Dec 15, 2014 at 9:41 PM
Soundcard drivers, although motherboard ones might help too, if you are using onboard soundcards like Realtek, which have had issues with NAudio in the past. Personally, I don't use DirectSoundOut at all, so I don't really have a feel for its long-term stability. From the stack-trace, the Access Violation is happening in the call to Stop, so possibly something has already gone wrong that causes playback to unexpectedly stop at this point. Things like the computer hibernating / sleeping, or usb soundcards being unplugged could possibly cause this type of crash.
Dec 17, 2014 at 11:28 PM
I'm using WaveOutEvent and get this error.
Coordinator
Dec 28, 2014 at 2:51 PM
what's the stacktrace when you use WaveOutEvent? Is it calling Stop again?
Is it possible for you to use regular WaveOut (with Windowed callbacks) instead?

Also, are you getting PlaybackStopped event, and if so, is the Exception property set to anything?
Jan 26, 2015 at 3:03 PM
I am also getting this error at times when I call the Stop method of the AsioOut.cs class, which is in turn calling asioDriverVTable.stop(pASIOComObject) in the AsioDriver class. In fact, in AsioOut.cs->driver_BufferUpdate, I had to comment out the Stop() call within if (read == 0) because the application would hang or crash there with all three ASIO drivers I tried.
Coordinator
Feb 20, 2015 at 7:54 PM
what soundcard are you using? maybe we should add an option to not auto-stop when the end of the input stream is reached, and just keep playing silence, and let the calling application call Stop when it is ready
Feb 21, 2015 at 5:07 PM
markheath wrote:
what soundcard are you using? maybe we should add an option to not auto-stop when the end of the input stream is reached, and just keep playing silence, and let the calling application call Stop when it is ready
I've had problems when using the ASIO4All driver, the driver for my Prism Sound Orpheus, and the TUSB Audio ASIO driver that ships with some USB to SPDIF converters.

Regarding the option to not autostop, I think that is a good idea. I've been doing something similar because one of the software ASIO drivers I use is a convolution engine for room correction, and there is quite a lag as it refills its buffer after a stop. So I've moved to using the MixingSampleProvider to keep from stopping between songs. The only time I specifically call stop is if I am switching sample rates. Otherwise I use a class similar to that AutoDisposeFileReader I found in one of your blog posts, and add a new mixer input when it is time to play a new track.