cefsharp / CefSharp

.NET (WPF and Windows Forms) bindings for the Chromium Embedded Framework
http://cefsharp.github.io/
Other
9.79k stars 2.92k forks source link

WinForms - NullReferenceException in ChromiumWebBrowser.OnHandleDestroyed #3931

Closed SirSparkles closed 2 years ago

SirSparkles commented 2 years ago

96.0.142

WinForms


StackTrace

System.NullReferenceException: Object reference not set to an instance of an object. at CefSharp.WinForms.ChromiumWebBrowser.OnHandleDestroyedInternal() at CefSharp.WinForms.ChromiumWebBrowser.OnHandleDestroyed(EventArgs e) at System.Windows.Forms.Control.WmDestroy(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

See https://github.com/TV-Rename/tvrename/issues/871 for the stack trace we received in our app

amaitland commented 2 years ago

I've added a basic check that should avoid the NRE. It's still possible your code might have some problems, without an example that reproduces the problem I can only speculate.

Best guess would be that OnHandleDestroyed is being called before the underlying CEF Browser is actually created (CreateBrowser is async call). Reparenting the the underlying CEF Browser simply wouldn't be possible in this case.

Are you moving the ChromiumWebBrowser to a different parent when this happens?

SirSparkles commented 2 years ago

Looking through the logs there does seem to be a higher proportion of this error occurring when the program is run in headless mode (the UI is still created, but hidden). They also occur with in 1-4 seconds of application starting, so probably it has not had time to fully initialise yet?

amaitland commented 2 years ago

They also occur with in 1-4 seconds of application starting, so probably it has not had time to fully initialise yet?

Seems likely. What causes the handle to be recreated?