Honza Bednář
Honza Bednář

Reputation: 406

google v8 persistent Context - script compilation crashes

I'm trying to use one global context for later executions. When the function1 is called, script returns the correct value. However when I call function2 after that, Script::Compile crashes. Function 1 is an initialization of v8 so I call it only once.

global variables:

    Isolate *isolate;
    Persistent<Context> pContext;

code for function1:

    auto platform = platform::NewDefaultPlatform();
    V8::InitializePlatform(platform.get());
    V8::Initialize();

    Isolate::CreateParams create_params;
    create_params.array_buffer_allocator = ArrayBuffer::Allocator::NewDefaultAllocator();
    isolate = Isolate::New(create_params);

    Locker locker(isolate);

    Isolate::Scope isolate_scope(isolate);
    HandleScope handle_scope(isolate);

    Local<Context> context = Context::New(isolate);
    pContext.Reset(isolate, context);

    Context::Scope context_scope(context);

    Local<Script> script = Script::Compile(context, to_v8(isolate, "'Test'")).ToLocalChecked();

    Local<Value> r = script->Run(context).ToLocalChecked();

    cout << "script result: " << *(String::Utf8Value(isolate, r));

function2:

Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);

Local<Context> context = Local<Context>::New(isolate, pContext);

Local<Script> script = Script::Compile(context, to_v8(isolate, "'Test'")).ToLocalChecked();

Context::Scope context_scope(context);
Local<Value> r = script->Run(context).ToLocalChecked();

cout << "script result: " << *(String::Utf8Value(isolate, r));

Stacktrace I'm getting:

at v8::internal::Compiler::GetSharedFunctionInfoForScript[v8::internal::Isolate*, v8::internal::Handle<v8::internal::String>, v8::internal::Compiler::ScriptDetails const&, v8::ScriptOriginOptions, v8::Extension*, v8::internal::ScriptData*, v8::ScriptCompiler::CompileOptions, v8::ScriptCompiler::NoCacheReason, v8::internal::NativesFlag] (UnknownFile:?)
at v8::ScriptCompiler::CompileUnboundInternal[v8::Isolate*, v8::ScriptCompiler::Source*, v8::ScriptCompiler::CompileOptions, v8::ScriptCompiler::NoCacheReason] (UnknownFile:?)
at v8::ScriptCompiler::Compile[v8::Local<v8::Context>, v8::ScriptCompiler::Source*, v8::ScriptCompiler::CompileOptions, v8::ScriptCompiler::NoCacheReason] (UnknownFile:?)
at v8::Script::Compile[v8::Local<v8::Context>, v8::Local<v8::String>, v8::ScriptOrigin*] (UnknownFile:?)

to_v8 is a function for v8::String::NewFromUtf8()

Upvotes: 0

Views: 382

Answers (1)

jmrk
jmrk

Reputation: 40561

Looks like this is the same problem as Embedded V8 context and script compilation crash: v8::platform::NewDefaultPlatform returns a std::unique_ptr (which you don't see due to your use of auto), so it gets destroyed when it goes out of scope. Make that a global variable and things should work.

Note 1: if you compiled a Debug build and used a Debugger, you would be able to figure out such problems yourself, which would save you a lot of time compared to having to post a question somewhere and then waiting for someone to get around to answering it.

Note 2: Be careful with auto. Types are meaningful; seeing them (by having them spelled out) helps avoid bugs, as this question illustrates.

Upvotes: 3

Related Questions