xenolightning / AudioSwitcher

.NET Library which facilitates interacting with Audio Devices on Windows
Microsoft Public License
248 stars 53 forks source link

Strange exception: msvcrt.dll #31

Open osbeorn opened 7 years ago

osbeorn commented 7 years ago

Hello,

I randomly encounter the following error in my program when using this library:

Exception thrown at 0x00007FF81DD03F80 (msvcrt.dll) in FireDeptStopwatch.exe: 0xC0000005: Access violation reading location 0x0000000014585ED8.

The following line of code is enough to trigger the exception: var sessions = audioController.DefaultPlaybackDevice.GetCapability<IAudioSessionController>();

Any clue what might be wrong?

xenolightning commented 7 years ago

What operating system are you running on?

Does it happen immediately, or after the application has been running for some time?

osbeorn commented 7 years ago

Win10 x64. It happens after sometime. And it seems quite random.

xenolightning commented 7 years ago

The underlying COM object may get destroyed.

Taking a quick look at the session code, there may be some enhancements in that space regarding object lifecycle.

osbeorn commented 7 years ago

One more thing ... My application is basically a stopwatch, which plays a command on start and plays another random audio track on end. It seems that just one specific audio track triggers this error.

The audio track is a 10s long .wav file of some song. The code used to play the track: using (var player = new SoundPlayer((UnmanagedMemoryStream) sound)) { player.Play(); }

xenolightning commented 7 years ago

Are you able to create a gist or attach a piece of self contained code that can be used to reproduce it? If you can, it should be fairly easy to track down and fix.

osbeorn commented 7 years ago

Here you go: fireDeptStopwatchGist.zip

Run the project, press Start, wait for the counter to start counting and then press Stop. You should get the mentioned exception.

osbeorn commented 7 years ago

It seems that the main problem is this block of code: using (var player = new SoundPlayer((UnmanagedMemoryStream) sound)) { player.Play(); }

If I remove the using block and normally create a new SoundPlayer instance, the application stops crashing. It seems the SoundPlayer instance created inside the using block is getting disposed before it finishes playing the sound.

But if I remove your library, the SoundPlayer works fine even when using it with the using block.

xenolightning commented 7 years ago

I had a look at this in the weekend, and it's definitely in the Session Initialization code.

I should be able to track it down, and make a fix for it. I'll tag the commit in here, once it's done.

Cheers.