Issues in cefshutdown

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.

Issues in cefshutdown

Postby chayal » Thu Oct 28, 2021 1:35 pm

Hi,
I have an MFC application and I embed CEF based browser in the application.
It supports following operations in following order:
1. Launch application .
2. Open a file/create a new.
3. View HTML page.
4. Close file.
5. Exit application.

So no CEF process is created in step 1. I am creating the window layout in step 2 and that is where I am creating the cef browser. I am able to fork all the sub processes in this step.
In step 3, View HTML , I basically create a post request and get the HTML response which is rendered.
Now my problem is, When i close file(step 4), I want cef processes to be killed by calling cef shutdown.Render process is killed but other process keep on running. If try cefshutdown, my application hangs with excepetions.. If I exit the application(step 4), I am able to close the cef processes using cefshutdown.

Why I want is cef processes to be killed in step 4:
Because I can again call step 2 after step 4. i.e close an already opened file and open a new file. When I do this way, since the cef processes were not killed in the close process, they are not being called again.


Some code for reference from the main application:

CefSettings cefsettings;

cefsettings.log_severity = LOGSEVERITY_VERBOSE;

// initialize CEF
HINSTANCE hInstance = GetModuleHandle(NULL);
CefMainArgs main_args(hInstance);
CefRefPtr<CefCommandLine> cmd_line = CefCommandLine::CreateCommandLine();

CefWindowInfo window_info;
window_info.SetAsChild(m_wndBrowser, oRect);
app = new SimpleCefApp(window_info);
CefString process_type;
app->OnBeforeCommandLineProcessing(process_type, cmd_line);

void* sandbox_info = nullptr;
cefsettings.multi_threaded_message_loop = true;
cefsettings.log_severity = LOGSEVERITY_VERBOSE;
CefString(&cefsettings.browser_subprocess_path).FromASCII("cefsimplehelper.exe");

const auto exit_code = CefExecuteProcess(main_args, app.get(), nullptr);
if (exit_code >= 0)
return exit_code;

BOOL stat = CefInitialize(main_args, cefsettings, app.get(), sandbox_info);
if (!CEFInitialized)
{
CEFInitialized = true;
}
chayal
Techie
 
Posts: 25
Joined: Sat Oct 02, 2021 4:50 pm

Re: Issues in cefshutdown

Postby magreenblatt » Thu Oct 28, 2021 2:34 pm

Now my problem is, When i close file(step 4), I want cef processes to be killed by calling cef shutdown.Render process is killed but other process keep on running. If try cefshutdown, my application hangs with excepetions.. If I exit the application(step 4), I am able to close the cef processes using cefshutdown.

CEF can only be initialized and shut down a single time in a given process. You can, however, create multiple CefBrowser instances at different times.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Issues in cefshutdown

Postby chayal » Thu Oct 28, 2021 3:09 pm

Thanks for the reply.
I am creating cef browser in oncontextinitialized() implemented in Simple_app. OnContextInitialized as a callback when cef is initialized. Which would be correct place to create cef browsers then.

Reference code:
void SimpleCefApp::OnContextInitialized()
{
CEF_REQUIRE_UI_THREAD();
// SimpleHandler implements browser-level callbacks.
CefRefPtr<SimpleCefClient> handler(new SimpleCefClient(false));

CefBrowserSettings browser_settings;

std::string url;
CefRefPtr<CefCommandLine> command_line =
CefCommandLine::GetGlobalCommandLine();

// Check if a "--url=" value was provided via the command-line. If so, use
// that instead of the default URL.
url = command_line->GetSwitchValue("url");
if (url.empty())
url = "about:blank";



#if defined(OS_WIN)
// On Windows we need to specify certain flags that will be passed to
// CreateWindowEx().
//window_info.SetAsPopup(NULL, "cefsimple");
#endif

// Create the first browser window.
bool retVal = CefBrowserHost::CreateBrowser(m_wndInfo, handler, url, browser_settings,
nullptr, nullptr);


}
chayal
Techie
 
Posts: 25
Joined: Sat Oct 02, 2021 4:50 pm

Re: Issues in cefshutdown

Postby magreenblatt » Thu Oct 28, 2021 3:11 pm

chayal wrote:Which would be correct place to create cef browsers then.

If you are hosting the browser in a dialog/window, then you might create it when the dialog/window is created.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Issues in cefshutdown

Postby chayal » Fri Oct 29, 2021 7:04 am

Thanks for the reply. I have put the check to initialize the cef only once.
But when I try to create browser again, I am not able to get call back at OnAfterCreated due to which I am unable to get the reference browser. Sharing my code modifications:

if (!CEFInitialized)
{
// cef initialization code as earlier
CEFInitialized = true;
}
else
{
app->OnContextInitialized(); //Creating browser in this function but not getting call back at OnAfterCreated.
}
chayal
Techie
 
Posts: 25
Joined: Sat Oct 02, 2021 4:50 pm

Re: Issues in cefshutdown

Postby magreenblatt » Fri Oct 29, 2021 9:06 am

Does the first browser work as expected? How are you running the CEF message loop? Where are you calling CefShutdown?
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Issues in cefshutdown

Postby chayal » Fri Oct 29, 2021 12:29 pm

Yes, the first browser is running as expected.
I am not running CefMessageLoop since I am setting multi_threaded_message_loop as true.
Also I am calling cefshutdown when I exit the application(step 5)
chayal
Techie
 
Posts: 25
Joined: Sat Oct 02, 2021 4:50 pm

Re: Issues in cefshutdown

Postby chayal » Sun Oct 31, 2021 10:47 am

I think we don’t run message loop when in milti threaded message loop.
I guess if the tender process is created, then there must be some browser associat with it. I just need to get that browser
chayal
Techie
 
Posts: 25
Joined: Sat Oct 02, 2021 4:50 pm

Re: Issues in cefshutdown

Postby magreenblatt » Sun Oct 31, 2021 1:04 pm

Check that you're passing the correct CefClient instance into CreateBrowser, and that you get callbacks with that CefClient instance.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

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