CefSharp icon indicating copy to clipboard operation
CefSharp copied to clipboard

NullReferenceException in ILifeSpanHandler.DoClose

Open ncksol opened this issue 9 years ago • 7 comments

  • What version of the product are you using?

    • 55.0.0 Nuget
  • What architecture x86 or x64?

    • x86
  • On what operating system?

    • Win10
  • Are you using WinForms, WPF or OffScreen?

    • WPF
  • What steps will reproduce the problem?

    • We have custom implementation of ILifeSpanHandler to make sure popups are opened inside the main window. The implementation of DoClose method I took from WinForms.Example:
        public bool DoClose(IWebBrowser browserControl, IBrowser browser)
        {
            //We need to allow popups to close
            //If the browser has been disposed then we'll just let the default behaviour take place
            if (browser.IsDisposed || browser.IsPopup)
            {
                return false;
            }

            //The default CEF behaviour (return false) will send a OS close notification (e.g. WM_CLOSE).
            //See the doc for this method for full details.
            //return true here to handle closing yourself (no WM_CLOSE will be sent).
            return true;
        }

Now we are getting the following exception, when users are closing the browser window:

Exception Info: System.NullReferenceException
   at CefSharp.Internals.CefSharpBrowserWrapper.get_IsPopup()
   at Invenias.WebControl.Handlers.InveniasLifeSpanHandler.DoClose(CefSharp.IWebBrowser, CefSharp.IBrowser)
   at <Module>.CefSharp.Internals.ClientAdapter.DoClose(CefSharp.Internals.ClientAdapter*, CefRefPtr<CefBrowser>*)

It is definitely something that is time dependant as I was't able to reproduce it consistently. It looks like the code checks that browser is disposed finds that it is not and goes ahead to check whether it's a popup. But meanwhile the browser gets disposed and therefore the errors is thrown. Is that possible?

ncksol avatar Feb 23 '17 15:02 ncksol

>	CefSharp.Core.dll!CefSharp::Internals::CefSharpBrowserWrapper::get_IsPopup() Line 140	C++
 	CefSharp.Wpf.Example.exe!CefSharp.Wpf.Example.Handlers.LifespanHandler.CefSharp.ILifeSpanHandler.DoClose(CefSharp.IWebBrowser browserControl, CefSharp.IBrowser browser) Line 100	C#
 	CefSharp.Core.dll!CefSharp::Internals::ClientAdapter::DoClose(CefRefPtr<CefBrowser>* browser) Line 198	C++
 	[Native to Managed Transition]	
 	CefSharp.Core.dll!CefWindowInfoTraits::init(struct _cef_window_info_t *)	Unknown
 	libcef.dll!0766a48d()	Unknown
 	[Frames below may be incorrect and/or missing, no symbols loaded for libcef.dll]	
 	libcef.dll!07c870c2()	Unknown
 	libcef.dll!07c86fec()	Unknown
 	libcef.dll!07c8539a()	Unknown
 	libcef.dll!07c8c7ed()	Unknown
 	libcef.dll!069ddb74()	Unknown
 	libcef.dll!06998a57()	Unknown
 	libcef.dll!069bf3e1()	Unknown

merceyz avatar Feb 23 '17 18:02 merceyz

https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Core/Internals/ClientAdapter.cpp#L190 https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Core/Internals/ClientAdapter.cpp#L211

Need to update ClientAdapter::DoClose to be more like ClientAdapter::OnBeforeClose

amaitland avatar Feb 23 '17 20:02 amaitland

Creating a new CefSharpBrowserWrapper as in OnBeforeClose solved the NullReferenceException but caused other problems, including:

  • The subprocess doesn't close when a tab is closed
  • Closing the main process causes the subprocess to crash
Application: CefSharp.BrowserSubprocess.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.PipeException
   at System.ServiceModel.Channels.PipeConnection.StartSyncWrite(Byte[], Int32, Int32, System.Object ByRef)
   at System.ServiceModel.Channels.PipeConnection.WriteHelper(Byte[], Int32, Int32, Boolean, System.TimeSpan, System.Object ByRef)

Exception Info: System.ServiceModel.CommunicationException
   at System.ServiceModel.Channels.PipeConnection.WriteHelper(Byte[], Int32, Int32, Boolean, System.TimeSpan, System.Object ByRef)
   at System.ServiceModel.Channels.PipeConnection.Write(Byte[], Int32, Int32, Boolean, System.TimeSpan)
   at System.ServiceModel.Channels.BufferedConnection.WriteNow(Byte[], Int32, Int32, System.TimeSpan, System.ServiceModel.Channels.BufferManager)
   at System.ServiceModel.Channels.BufferedConnection.Write(Byte[], Int32, Int32, Boolean, System.TimeSpan)
   at System.ServiceModel.Channels.FramingDuplexSessionChannel.CloseOutputSessionCore(System.TimeSpan)
   at System.ServiceModel.Channels.TransportDuplexSessionChannel.CloseOutputSession(System.TimeSpan)
   at System.ServiceModel.Channels.TransportDuplexSessionChannel.OnClose(System.TimeSpan)
   at System.ServiceModel.Channels.CommunicationObject.Close(System.TimeSpan)
   at System.ServiceModel.Channels.ServiceChannel.OnClose(System.TimeSpan)
   at System.ServiceModel.Channels.CommunicationObject.Close(System.TimeSpan)
   at System.ServiceModel.Channels.ServiceChannelFactory.OnClose(System.TimeSpan)
   at System.ServiceModel.Channels.ServiceChannelFactory+TypedServiceChannelFactory`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].OnClose(System.TimeSpan)
   at System.ServiceModel.Channels.CommunicationObject.Close(System.TimeSpan)
   at System.ServiceModel.ChannelFactory.OnClose(System.TimeSpan)
   at System.ServiceModel.Channels.CommunicationObject.Close(System.TimeSpan)
   at System.ServiceModel.Channels.CommunicationObject.Close()
   at CefSharp.BrowserSubprocess.WcfEnabledSubProcess.OnBrowserDestroyed(CefSharp.CefBrowserWrapper)
   at <Module>.CefSharp.CefAppUnmanagedWrapper.OnBrowserDestroyed(CefSharp.CefAppUnmanagedWrapper*, CefRefPtr<CefBrowser>*)
   at <Module>.CefExecuteProcess(CefMainArgs*, CefRefPtr<CefApp>*, Void*)
   at CefSharp.BrowserSubprocess.SubProcess.Run()
   at CefSharp.BrowserSubprocess.Program.Main(System.String[])

merceyz avatar Feb 24 '17 19:02 merceyz

Need to update ClientAdapter::DoClose to be more like ClientAdapter::OnBeforeClose

I was mistaken about this, no need to change it as DoClose is called first

The subprocess doesn't close when a tab is closed Closing the main process causes the subprocess to crash

@merceyz What exactly did your code look like? Only reason I can think of that would cause this sort of problem is leaving unamanged references hanging around.

We have custom implementation of ILifeSpanHandler to make sure popups are opened inside the main window. The implementation of DoClose method I took from WinForms.Example:

@ncksol Can you provide your whole ILifeSpanHandler implementation? Need a way to reproduce the problem (fork the main project or the MinimalExample).

An important question to ask is the code block you've copied from the WinForms example relevant in the context of a WPF application?

amaitland avatar Mar 05 '17 23:03 amaitland

What exactly did your code look like?

@amaitland It was because of the return true in the implementation of ILifeSpanHandler specified in the issue

merceyz avatar Mar 08 '17 21:03 merceyz

@amaitland my ILifeSpanHandler doesn't really have much code. The only reason I've created my own implementation is to prevent popups from being opened in a new window. To achieve that I just took the code from one of the example here. Here is a gist - https://gist.github.com/ncksol/82c5e87ed334329ad10cafdfc52f7a97

I've only copied the functionality from WinForms, because the previous implementation I had caused the form with CefSharp in it to hang the whole application for a couple seconds on close. I've checked the history of changes for WinForms and WPF examples and assumed that the changes from WinForms just haven't been copied into WPF example.

ncksol avatar Mar 09 '17 10:03 ncksol

@ncksol If you were to return false in DoClose without checking anything your problem would be solved

merceyz avatar Mar 09 '17 13:03 merceyz