Chromium built-in framework: object creation fails when using "ExecuteFunctionWithContext"


I am using Chrome Built Infrastructure (cef) on Delphi 2009, this is the latest version.


I can use ExecuteFunctionWithContext

JavaScript callback routines to execute successfully and I can provide it with arguments. However, when I try to use TCefv8ValueRef.CreateObject(nil);

, an access violation happens in the libcef library.


  • Hitting an argument for a JavaScript callback works for TCefv8ValueRef.CreateString

    all other types as well Cefv8Value

  • The function TCefv8ValueRef.CreateObject(nil)

    works great when used as a return value for chrome extension. (As described in the demo /demos/guiclient

    for Delphi CEF).
  • The object TChromium

    is stored in the main form.

Possible solutions and thoughts

  • I tried to use TCefv8ValueRef.CreateObject(nil);

    via an event OnClick

    on the main form, it also resulted in an access violation. However TCefv8ValueRef.CreateString('test');

    will work fine.

Any help would be greatly appreciated.


source to share

1 answer

I had the same problem in C ++! And I solved it with the following code:

CefRefPtr<CefFrame> frame = browser->GetMainFrame();
CefRefPtr<CefV8Context> v8Context = frame->GetV8Context();
if (v8Context.get() && v8Context->Enter())
    CefRefPtr<CefV8Value> object = CefV8Value::CreateObject(NULL);
    // ExecuteFunctionWithContext and other actions



The chrome-treated documentation contains the following:

So, you have to include the correct combination before your actions using javascript model. If V8 is not currently inside the context, or if you need to restore and save a reference to the context, you can use one of the two available static methods of CefV8Context. GetCurrentContext () returns the context for the frame that JS is currently executing. GetEnteredContext () returns the context for the frame where JS execution started. For example, if a function in frame1 calls a function in frame2, then the current context will be frame2 and the entered context will be frame1.

Arrays, objects and functions can be created, modified and, in the case of executing functions, if V8 is inside a context. If V8 is not within context, then the application must enter the context by calling Enter () and exit the context by calling Exit (). The Enter () and Exit () methods should only be used:

  • When creating a V8 object, function, or array outside of an existing context. For example, when creating a JS object in response to a custom menu callback.

  • When creating a V8 object, function, or array in a context other than the current context. For example, if a call originating from frame1 should change the context of frame2.

So why couldn't you create an object but were able to create js strings. Also you can see a general usage example .



All Articles