Object reference not set to an instance of an object when resizing window
Summary
Exceptions thrown when using a docking framework (such as Infragistics Dock Manager). The video still carries on playing and everything works, it's just that some unhandled exceptions are thrown as you tear out and re-dock windows, presumably as their parent changes. Note that the example requires the use of an Infragistics library which I am not able to provide, but which you can create a trial account for to see the expected result.
Minimal project and steps to reproduce
In the attached project, you need to click on one of the tabs and drag it out, re-dock it, repeat. Before doing so, make sure you have all exception catching checked in Visual Studio. Also, inside Main.xaml.cs you need to replace @"ADD VIDEO LINK HERE" with an actual video.
What is the current bug behavior?
My feeling is that as you drag components out and re-dock them, the window owner changes, which causes two kinds of exception to be thrown. To be clear, they don't always both happen deterministically, but are still pretty easy to reproduce.
-
The issue in this case is that in this 'Wndhost_SizeChanged' we call 'var source = PresentationSource.FromVisual(_wndhost);' but source in these cases ends up being assign to null, which subsequently causes this exception to be thrown.
System.NullReferenceException: Object reference not set to an instance of an object. at LibVLCSharp.WPF.ForegroundWindow.Wndhost_SizeChanged(Object sender, SizeChangedEventArgs e)
-
This is the second error we see, which is effectively because in the method 'Background_Loaded' at the end of the try statement we try to call Show(), but the window has already been hidden and so according to WPF this operation is illegal. The reason the exception below is misleading is because the code reads 'throw new VLCException("Unable to create WPF Window in VideoView.");' without re-propagating the actual exception that led to this point.
LibVLCSharp.Shared.VLCException: Unable to create WPF Window in VideoView. at LibVLCSharp.WPF.ForegroundWindow.Background_Loaded(Object sender, RoutedEventArgs e)
What is the expected correct behavior?
Exceptions not being thrown, otherwise it is working perfectly well.
Does it work on other plaforms? Does it work with the official VLC apps?
This is to do with the WPF wrapper and not with VLC.
Relevant logs and/or screenshots
Exception 1
Exception 2
Environment
- OS: Windows 10 x64 1909
- Device: Windows machine
- LibVLC 3.0.11, x64
- LibVLCSharp 3.4.6
Possible fixes
- In 'Wndhost_SizeChanged', if source is assigned to null we need a null guard to break out of the method
- In 'Background_Loaded' we should not attempt to call Show() once the Window has already been closed, so this could be a symptom of the Loaded event having fired at an unexpected moment
- In 'Background_Loaded' in general it would be useful to catch the exception and include in the re-propagated exception, otherwise any exceptions caught here are misleading as we discard any information that led to this point