CefInitialize won't work from a mfc dll

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.

CefInitialize won't work from a mfc dll

Postby sirneromani » Fri Aug 22, 2014 6:02 am

I want to use Chromium Embeded Framework in an mfc dll. Therefore I created a mfc test exe and tested a minimalistic implementation of cef (which worked). My next step was to do exactly the same thing with a test mfc dll. The code is almost the same but the cef browser window won't come up.

The application hangs in CefInitialize() and won't return. After debugging, I found that it gets stuck in a WaitForSingleObject/WaitForMultipleObject windows api.

Code: Select all
BOOL CMyBrowserDllApp::InitInstance()
{
    HINSTANCE hInstance = GetModuleHandle(NULL);

    CefMainArgs main_args(hInstance);

    CefSettings settings;
    settings.no_sandbox = true;
    settings.multi_threaded_message_loop = true;

    // Execute the secondary process, if any.
    int exit_code = CefExecuteProcess(main_args, cefApplication.get(), NULL);
    if (exit_code >= 0)
        return exit_code;

    if(!CefInitialize(main_args, settings, cefApplication.get(), NULL))
    {
        OutputDebugStringA("Error: CefInitialize failed");
    }else
        OutputDebugStringA("Info: CefInitialize succeeded");

    CWinApp::InitInstance();

    return TRUE;
}


Same implementation works if used in a mfc exe directly. Both projects link statically to mfc dlls.
sirneromani
Newbie
 
Posts: 8
Joined: Fri Aug 22, 2014 5:58 am

Re: CefInitialize won't work from a mfc dll

Postby magreenblatt » Fri Aug 22, 2014 8:23 am

sirneromani wrote:IThe application hangs in CefInitialize() and won't return. After debugging, I found that it gets stuck in a WaitForSingleObject/WaitForMultipleObject windows api.

What CEF version? What is the call stack?
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: CefInitialize won't work from a mfc dll

Postby sirneromani » Fri Aug 22, 2014 8:38 am

Version is 3.1750.1738

I put all in a vs project now and can debug in the code. I can follow the code until the method "Run" in content_main_runner.cc. From there on the sources seem not to fit to my branch. However, I tried to change to the relevant branch, but as it seems, without success. Btw. is it normal, that the whole checkout has 15 gig and takes ages (same for the branch switch). Isn't there a faster and more convenient way, because I just need some source files for debugging.

Anyhow, when I run into the endless loop/deadlock/hang the call stack is already wiped out.
sirneromani
Newbie
 
Posts: 8
Joined: Fri Aug 22, 2014 5:58 am

Re: CefInitialize won't work from a mfc dll

Postby sirneromani » Tue Sep 02, 2014 7:10 am

Now I managed to get the appropriate source code and could dig deeper. The code gets stuck in the "WaitForSingleObject()" in the following piece of code (waitable_event_win.cc).

Code: Select all
void WaitableEvent::Wait() {
  base::ThreadRestrictions::AssertWaitAllowed();
  DWORD result = WaitForSingleObject(handle_, INFINITE);
  // It is most unexpected that this should ever fail.  Help consumers learn
  // about it if it should ever fail.
  DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed";
}


For me the unexpected arrived, so could u please help me learn about it. :-)

Seems like a deadlock, even visual studio tells me, when I use "Break All". At the time of the lock I got three threads running. Looks like all got stuck in a "WaitForSingleObject()". I would really apprechiate if anyone could help me on this.

    Stacktrace Main Thread: "Main Thread":
    ntdll.dll!_NtWaitForSingleObject@12() + 0x15 bytes
    ntdll.dll!_NtWaitForSingleObject@12() + 0x15 bytes
    kernel32.dll!_WaitForSingleObjectExImplementation@12() + 0x43 bytes
    kernel32.dll!_WaitForSingleObject@8() + 0x12 bytes
    > libcef.dll!base::WaitableEvent::Wait() Line 52 + 0x10 bytes C++
    libcef.dll!base::Thread::StartWithOptions(const base::Thread::Options & options={...}) Line 117 C++
    libcef.dll!CefMainDelegate::RunProcess(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & process_type="", const content::MainFunctionParams & main_function_params={...}) Line 415 + 0x13 bytes C++
    libcef.dll!content::RunNamedProcessTypeMain(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & process_type="", const content::MainFunctionParams & main_function_params={...}, content::ContentMainDelegate * delegate=0x00985e60) Line 462 + 0x17 bytes C++
    libcef.dll!content::ContentMainRunnerImpl::Run() Line 793 + 0x14 bytes C++
    libcef.dll!CefContext::Initialize(const CefMainArgs & args={...}, const CefStructBase<CefSettingsTraits> & settings={...}, CefRefPtr<CefApp> application={...}, void * windows_sandbox_info=0x0017edc4) Line 290 + 0x29 bytes C++
    libcef.dll!CefInitialize(const CefMainArgs & args={...}, const CefStructBase<CefSettingsTraits> & settings={...}, CefRefPtr<CefApp> application={...}, void * windows_sandbox_info=0x00000000) Line 127 + 0x23 bytes C++
    libcef.dll!cef_initialize(const _cef_main_args_t * args=0x0017f3c8, const _cef_settings_t * settings=0x0017f314, _cef_app_t * application=0x00438120, void * windows_sandbox_info=0x00000000) Line 165 + 0x24 bytes C++
    MyBrowserDll.dll!CefInitialize(const CefMainArgs & args={...}, const CefStructBase<CefSettingsTraits> & settings={...}, CefRefPtr<CefApp> application={...}, void * windows_sandbox_info=0x00000000) Line 157 + 0x59 bytes C++
    MyBrowserDll.dll!CMyBrowserDllApp::InitInstance() Line 86 + 0x35 bytes C++
    MyBrowserDll.dll!InternalDllMain(HINSTANCE__ * hInstance=0x0f7b0000, unsigned long dwReason=1, void * __formal=0x00000000) Line 100 + 0x13 bytes C++
    MyBrowserDll.dll!DllMain(HINSTANCE__ * hInstance=0x0f7b0000, unsigned long dwReason=1, void * lpReserved=0x00000000) Line 272 C++
    MyBrowserDll.dll!__DllMainCRTStartup(void * hDllHandle=0x0f7b0000, unsigned long dwReason=1, void * lpreserved=0x00000000) Line 330 + 0x11 bytes C
    MyBrowserDll.dll!_DllMainCRTStartup(void * hDllHandle=0x0f7b0000, unsigned long dwReason=1, void * lpreserved=0x00000000) Line 293 + 0x11 bytes C
    ntdll.dll!_LdrpCallInitRoutine@16() + 0x14 bytes
    ntdll.dll!_LdrpRunInitializeRoutines@4() - 0x352 bytes
    ntdll.dll!_LdrpLoadDll@24() + 0x157b bytes
    ntdll.dll!_LdrLoadDll@16() + 0x7b bytes
    KernelBase.dll!_LoadLibraryExW@12() + 0x1f1 bytes
    KernelBase.dll!_LoadLibraryExA@12() + 0x26 bytes
    kernel32.dll!_LoadLibraryA@4() + 0x31 bytes
    RunDll.exe!wmain(int argc=1, wchar_t * * argv=0x007d1bb8) Line 10 + 0xd bytes C++
    RunDll.exe!__tmainCRTStartup() Line 552 + 0x19 bytes C
    RunDll.exe!wmainCRTStartup() Line 371 C
    kernel32.dll!@BaseThreadInitThunk@12() + 0x12 bytes
    ntdll.dll!___RtlUserThreadStart@8() + 0x27 bytes
    ntdll.dll!__RtlUserThreadStart@8() + 0x1b bytes

    Stacktrace WorkerThread: "sandbox::BrokerServicesBase::TargetEventsThread":
    > ntdll.dll!_NtWaitForSingleObject@12() + 0x15 bytes
    ntdll.dll!_NtWaitForSingleObject@12() + 0x15 bytes

    Stacktrace WorkerThread: "base::`anonymous namespace'::ThreadFunc":
    > ntdll.dll!_NtWaitForSingleObject@12() + 0x15 bytes
    ntdll.dll!_NtWaitForSingleObject@12() + 0x15 bytes
sirneromani
Newbie
 
Posts: 8
Joined: Fri Aug 22, 2014 5:58 am

Re: CefInitialize won't work from a mfc dll

Postby sirneromani » Fri Sep 05, 2014 9:24 am

I doubt, nobody has ever used CEF in a mfc dll before? Is there any additional information I could/should provide?
sirneromani
Newbie
 
Posts: 8
Joined: Fri Aug 22, 2014 5:58 am

Re: CefInitialize won't work from a mfc dll

Postby magreenblatt » Fri Sep 05, 2014 9:40 am

The call stack indicates that you're using multi-threaded-message-loop and that it's failing to start that initial thread. Is your application interfering with the creation of new threads in some way?
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: CefInitialize won't work from a mfc dll

Postby sirneromani » Fri Sep 05, 2014 9:55 am

My app does nothing so far. It is just a basic mfc dll fresh from the visual studio wizard. What could be the cause of the Thread failing?
sirneromani
Newbie
 
Posts: 8
Joined: Fri Aug 22, 2014 5:58 am

Re: CefInitialize won't work from a mfc dll

Postby magreenblatt » Fri Sep 05, 2014 10:09 am

How are you loading the dll? How is DllMain implemented?
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: CefInitialize won't work from a mfc dll

Postby sirneromani » Fri Sep 05, 2014 10:16 am

I'm just doing a LoadLibrary() on the dll. As far as I know, mfc wrapps DllMain() internaly.

Code: Select all
HINSTANCE hDll = LoadLibraryA("MyBrowserDll.dll");
sirneromani
Newbie
 
Posts: 8
Joined: Fri Aug 22, 2014 5:58 am

Re: CefInitialize won't work from a mfc dll

Postby magreenblatt » Fri Sep 05, 2014 10:20 am

OK, and what is the application calling LoadLibrary?
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Next

Return to Support Forum

Who is online

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