Handle browser lifespan when closing with Windows 'X'

Having problems with building or using the JCEF Java binding? Ask your questions here.

Handle browser lifespan when closing with Windows 'X'

Postby michael10 » Wed Aug 10, 2016 1:08 am

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]
Attachments
dump_200716_cefBrowser.log
(69.34 KiB) Downloaded 678 times
michael10
Newbie
 
Posts: 8
Joined: Fri May 13, 2016 2:34 am

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

Postby Czarek » Wed Aug 10, 2016 2:29 am

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.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

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

Postby michael10 » Wed Aug 10, 2016 10:13 am

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?
michael10
Newbie
 
Posts: 8
Joined: Fri May 13, 2016 2:34 am

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

Postby Czarek » Thu Aug 11, 2016 1:09 am

Make sure you're releasing all references to CEF objects *before closing browser / shutting down app.
Last edited by Czarek on Fri Aug 12, 2016 3:19 am, edited 1 time in total.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

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

Postby michael10 » Fri Aug 12, 2016 2:54 am

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);
            }
        }
michael10
Newbie
 
Posts: 8
Joined: Fri May 13, 2016 2:34 am

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

Postby Nidhi » Tue Nov 22, 2016 7:27 am

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.
Nidhi
Techie
 
Posts: 48
Joined: Fri Sep 02, 2016 6:23 am


Return to JCEF Forum

Who is online

Users browsing this forum: No registered users and 20 guests