filoe / cscore

An advanced audio library, written in C#. Provides tons of features. From playing/recording audio to decoding/encoding audio streams/files to processing audio data in realtime (e.g. applying custom effects during playback, create visualizations,...). The possibilities are nearly unlimited.
Other
2.17k stars 450 forks source link

NullreferenceException during MMNotificationClient.Dispose() #84

Closed digimezzo closed 8 years ago

digimezzo commented 8 years ago

I'm using MMNotificationClient to detect a change in the default audio device. When my class, which uses the MMNotificationClient object, gets disposed when closing my application, MMNotificationClient.Dispose() is called. During MMNotificationClient.Dispose() , this nullReferenceException occurs in class "MMDeviceEnumerator" during the return in this function:

public unsafe int UnregisterEndpointNotificationCallbackNative(IMMNotificationClient notificationClient)
        {
            return InteropCalls.CallI(UnsafeBasePtr, notificationClient, ((void**) (*(void**) UnsafeBasePtr))[7]);
        }

I've created a sample application that illustrates it: WpfApplication1

Steps to reproduce:

  1. Compile and launch the sample app
  2. Let the sample app start playing Test.mp3
  3. Close the sample app by pressing the close button on the window

=> the nullreferenceexception occurs

filoe commented 8 years ago

The "error" occurs when you don't dispose the _client object correctly.

digimezzo commented 8 years ago

Indeed, calling Dispose() on the NotificationClient resolves the issue. By the way, it seems the above fix doesn't prevent the NullReferenceException. _deviceEnumerator is not disposed when it happens, UnregisterEndpointNotificationCallbackNative still gets called and we get this: null pointer

filoe commented 8 years ago

Do have https://github.com/filoe/cscore/commit/cc383cdb31b63289f3db3662e7d910946ae7ce57?

digimezzo commented 8 years ago

No I missed that commit :( It's resolved now. As far as I'm concerned, this issue can be closed.