Page 1 of 1

Handle browser lifespan when closing with Windows 'X'

PostPosted: Wed Aug 10, 2016 1:08 am
by michael10
Our framework (main window) opens a separate window where we embed the CEF-Browser.
When user will close that child window by clicking the 'X', we are listen on the WN_CLOSE event in our main window and handling close event and we call cefBrowser.close() before removing the child-window
But we are running into "VM Access Violation" troubles when we are calling cefBrowser.close() and returning "true" in the CefLifeSpanHandler.doClose() method. (See dump_200716_cefBrowser.log)
Return "true" in doClose() means, that we do not want the second WN_CLOSE event from CEFBrowser, because we will close CefBrowser immediately.

Maybe it's an timing problem and we have to wait, while CEF is shutdown?
Or is it the same problem as described in https://bitbucket.org/chromiumembedded/java-cef/issues/226/windows-jvm-crashes-when-closing

I already read the comments in cef_life_span_handler.h and in https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage#markdown-header-browser-life-span, but I cant fix the problem with VM crashes.
It would be very helpful when there is a example, howto handle lifespan "correctly" in the CEF-Browser, when it is parented from another window.

Installing win32 JCEF Version = 3.2704.0 on Windows 10 win32:
Code: Select all
d:\depot\git-repository\java-cef2\src\tools>run.bat win32 Release detailed
Offscreen rendering disabled
Using:
JCEF Version = 3.2704.0
CEF Version = 3.2704.1427
Chromium Version = 51.0.2704.84
CefApp: INITIALIZING
initialize on Thread[AWT-EventQueue-0,6,main] with library path .\jcef_build\native\Release
Added scheme search://
Added scheme client://
CefApp: INITIALIZED
CefApp: SHUTTING_DOWN
  shutdown on Thread[AWT-EventQueue-0,6,main]

Re: Handle browser lifespan when closing with Windows 'X'

PostPosted: Wed Aug 10, 2016 2:29 am
by Czarek
If you don't need to support javascript's onunload/unbeforeunload events then you don't have to call CloseBrowser(). CEF browser will be closed automatically when parent window is destroyed.

Re: Handle browser lifespan when closing with Windows 'X'

PostPosted: Wed Aug 10, 2016 10:13 am
by michael10
Hi. Thanks for the reply!

Ok. So we also do not need CefLifeSpanHandler?
...
When I remove the cefBrowser.close() and CefLifeSpanHandler and
call javax.swing.JFrame.remove(compWithCefBrowser) in our main app, I am always getting an VM crash.
It seems there is a call from native to CefClient.onBeforeClose(CefBrowser browser) or CefClient.getBrowser(int identifier) which could not be handled anymore and so the the VM crashes.
I found out that it works mostly if we set debug breakpoints, but it always crashes without. Maybe a timing problem?

Re: Handle browser lifespan when closing with Windows 'X'

PostPosted: Thu Aug 11, 2016 1:09 am
by Czarek
Make sure you're releasing all references to CEF objects *before closing browser / shutting down app.

Re: Handle browser lifespan when closing with Windows 'X'

PostPosted: Fri Aug 12, 2016 2:54 am
by michael10
Thanks for info.
Removing the PropertyChangeListener KeyboardFocusManager.getCurrentKeyboardFocusManager().removePropertyChangeListener(cefClientPropertyChangeLlistener) before remove the UIComponent, works for us.

Code executed before we remove the UIComponent:
Code: Select all
        cefClient.removeDisplayHandler();
        cefClient.removeFocusHandler();
        cefClient.removeKeyboardHandler();
        cefClient.removeLifeSpanHandler();
        cefClient.removeRequestHandler();
        cefClient.removeLoadHandler();
        cefClient.removeMessageRouter(msgRouter);
       
        cefBrowser.setFocus(false);
        cefBrowser.getUIComponent().setFocusable(false);
        for(FocusListener F_fl : cefBrowser.getUIComponent().getFocusListeners())
        {
            cefBrowser.getUIComponent().removeFocusListener(F_fl);
        }
        PropertyChangeListener[] F_propertyChangeListeners = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPropertyChangeListeners();
        for(PropertyChangeListener F_pcl : F_propertyChangeListeners)
        {
            if(F_pcl.getClass().getName().contains("CefClient"))
            {
                KeyboardFocusManager.getCurrentKeyboardFocusManager().removePropertyChangeListener(F_pcl);
            }
        }

Re: Handle browser lifespan when closing with Windows 'X'

PostPosted: Tue Nov 22, 2016 7:27 am
by Nidhi
HI,
I am facing the same issue where pressing the 'X' button does not kill the render process immediately, causing my application to crash. Is there a way to kill the render process? Also, on pressing 'X' it is not calling DoClose function, it just calls the OnBeforeClose function.I am not using single process architecture.