MediaSessionCallback Access Violation #838
Description
I'm having a problem with the media session callback object used for asynchronously receiving media session events.
My problem is when shutting down the media session, in a similar manner as to seen here, I cannot safely dispose of the MediaSessionCallback
object without having a small chance of causing a fatal AccessViolation.
My code for shutting down works like follows:
MediaSession.Close()
is called- My thread then waits on 'close' event; similar to the MSDN example
- The
MediaSessionCallback
OnEvent
delegate receives theMESessionClosed
event and sets the 'close' event my thread is waiting on. MediaSession.Shutdown()
is called to make sure it releases any resources.MediaSession.Dispose()
is called to release the media session itself.MediaSessionCallback.Dispose()
is called to release the callback object.
The problem seems to be that if my thread gets to steps 5 and 6 too quickly, media foundation crashes with an access violation on the standard async callback work queue thread.
For testing purposes, the problem can be reproduced 100% of the time by cheating and bias the race condition by adding a 1ms delay to the MediaSessionCallback
function after setting the event, to make sure the thread lingers in the callback object a bit longer. Of course, doing the opposite, and putting a magic sleep before calling dispose on the media session & callback object, the probability of the crash can be reduced greatly. This is, however, a bit of a silly solution that would still give me no strong guarantees.
Am I doing something wrong with the MediaSessionCallback
object? I cannot see an obvious way to correctly detect when the callback thread has safely exited the callback object. This is causing me a bit of a headache because the software I'm writing has to create & destroy media sessions frequently, and also be running for long periods of time without crashing. If my explanation is not sufficient, please let me know. I could probably even write a small console application or something that demonstrates the problems I'm having in code.
Any ideas? Suggestions?
Thanks in advance,
Adam