exiting after CreateRootWindow() but before message_loop->Ru

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

exiting after CreateRootWindow() but before message_loop->Ru

Postby michaeladamkatz » Mon Mar 12, 2018 4:17 am

In my Windows CEF application I call

Code: Select all
context->GetRootWindowManager()->CreateRootWindow( window_config );


and then I do some application specific checks for which I need the window handle to already exist. Then depending on the results of those application specific checks I either call

Code: Select all
message_loop->Run();


or I just exit the app.

When I just exit the app I'm getting some assertions, specifically that RootWindowManager's root_windows_ is not empty, or that RootWindowWin's browser_destroyed_ or window_destroyed_ are false.

It appears that if I could call

Code: Select all
BrowserWindow::OnBrowserClosed(CefRefPtr<CefBrowser> browser)


it would do the right things to clean up all three of those variables. But I don't know how to access the current BrowserWindow (the active ClientHandler::Delegate?) to call OnBrowserClosed(). (And is this even the best way to handle this situation?)

I understand it might be better to only call CreateRootWindow() if I know I'm then going to call message_loop->Run(). But as I say, it would be most convenient for my app to call CreateRootWindow() and then possibly bail out.
michaeladamkatz
Mentor
 
Posts: 50
Joined: Wed Jan 09, 2013 5:10 pm

Re: exiting after CreateRootWindow() but before message_loop

Postby ndesktop » Mon Mar 12, 2018 6:15 am

The cleanup/shutdown process is not easy. Basically, you have to track the callbacks (On...Destroyed, On...Closed) and get out when the refcount drops to zero.
Here is my shutdown pseudocode (ClientHandler is the ... CEF client handler implementation, MainWindow is the main/root window; browser is CefRefPtr<CefBrowser>):
Code: Select all
ClientHandler::OnBeforeClose(browser)
  browsers_count--;
  _NotifyBrowserClosed(browser) - ensure TID_UI
    main window (root) => OnBrowserClosed

MainWindow::OnBrowserClosed(browser)
  browsers_destroyed = handler->browsers_count is 0
  if browsers_destroyed
    DestroyWindow(MainWindow.hWnd)
  if browsers_destroyed AND main_window_destroyed
    CefDoMessageLoopWork();
    CefQuitMessageLoop();

MainWindow::OnNCDestroy
  main_window_destroyed = true
  ...


So is short (in reality is a little bit more complicated, but let's keep the main idea simple):
1. ClientHandler notifies with OnBeforeClose for all CefBrowser (browser_count drops to zero)
2. MainWindow gets notified on browser destruction until reaches 0
3. MainWindow is destroyed (== WM_NCDESTROY handled by MainWindow)
4. when no more browsers exists and the main window has been destroyed, call CefDoMessageLoopWork one more time and finally CefQuitMessageLoop.
ndesktop
Master
 
Posts: 756
Joined: Thu Dec 03, 2015 10:10 am

Re: exiting after CreateRootWindow() but before message_loop

Postby magreenblatt » Mon Mar 12, 2018 12:22 pm

michaeladamkatz wrote:I understand it might be better to only call CreateRootWindow() if I know I'm then going to call message_loop->Run(). But as I say, it would be most convenient for my app to call CreateRootWindow() and then possibly bail out.

If your application is going to bail early you should do so before calling CreateRootWindow (or even before CefInitialize).
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: exiting after CreateRootWindow() but before message_loop

Postby magreenblatt » Mon Mar 12, 2018 12:24 pm

ndesktop wrote:4. when no more browsers exists and the main window has been destroyed, call CefDoMessageLoopWork one more time and finally CefQuitMessageLoop.

CefQuitMessageLoop is used with CefRunMessageLoop, not CefDoMessageLoopWork. See https://bitbucket.org/chromiumembedded/ ... ntegration
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: exiting after CreateRootWindow() but before message_loop

Postby ndesktop » Tue Mar 13, 2018 2:11 am

I was thinking to call once (not one more time as I wrote) CefDoMessageLoopWork in order to consume pending messages, if any.
I am using CefRunMessageLoop/CefQuitMessageLoop. Using CefDoMessageLoopWork (once, just before CefQuitMessageLoop) is a programming error then?
ndesktop
Master
 
Posts: 756
Joined: Thu Dec 03, 2015 10:10 am

Re: exiting after CreateRootWindow() but before message_loop

Postby magreenblatt » Tue Mar 13, 2018 12:59 pm

ndesktop wrote:I was thinking to call once (not one more time as I wrote) CefDoMessageLoopWork in order to consume pending messages, if any.
I am using CefRunMessageLoop/CefQuitMessageLoop. Using CefDoMessageLoopWork (once, just before CefQuitMessageLoop) is a programming error then?

It's not necessary but probably not harmful.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 111 guests