CsWinRT: System.ObjectDisposedException when using the MediaTranscoder class
I use the MediaTranscoder class to convert a stream of bitmaps to a video file. This works fine when using .net core 3.1, but fails when using .net 5. After supplying a few frames, a System.ObjectDisposedException is thrown in System.Private.CoreLib.dll. The stacktrace is shown below:
at media_transcoder_test.MainWindow.StartMediaTranscodeSession(String outputFile, UInt32 videoWidth, UInt32 videoHeight, UInt32 framerate) in media_transcoder_test\MainWindow.xaml.cs:line 128
at media_transcoder_test.MainWindow.Window_Loaded(Object sender, RoutedEventArgs e) in media_transcoder_test\MainWindow.xaml.cs:line 35
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__140_0(Object state)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run()
at media_transcoder_test.App.Main()
I have attached a repro project that generates up to 200 (empty) frames and tries to generate a mpeg-4 video out of them. When started using vscode it fails after around 100 frames on my machine. It also fails when started without debugging. Interestingly, this repro project finishes on my machine when running under the visual studio debugger so I suspect some kind of timing related issue. media_transcoder_test.zip
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 19 (10 by maintainers)
Can this bug be reopened? I can still reproduce it after upgrading to 1.1.3.
I upgraded to cswinrt 1.1.1, but I can still reproduce this issue.
@pjmagee - thanks to your repro, I think I’ve root caused and have a fix - the team is evaluating. In short, the .NET 5 runtime may occasionally hand out a disposed RCW for a given COM pointer, via ComWrappers.GetOrCreateObjectForComInstance. This happens most often with recycled COM pointers that are temporarily wrapped for callbacks (e.g., async handlers). The solution is to resurrect the disposed wrapper and reregister it with the GC.
Fix, using reflection (temporarily): https://github.com/microsoft/CsWinRT/compare/ujjwalchadha/fix-media-transcoder-error?expand=1#diff-61ead3a3c21696d411a1071c0e16e284e38c804c6fde4f68ddd2da7301e110d3
@pjmagee - never mind, my target window was minimized, causing the BitBlt to fail. I have the repro with ObjectDisposedException - thx.