为隔离创建新上下文时出现异常的原因是什么?

时间:2019-02-13 13:44:36

标签: v8 embedded-v8 libv8

给出的代码被执行了很多次,并与一个变量实例(g_creator,g_isolate)一起使用。 在第二次调用此代码时,函数v8 :: Context :: New中发生异常。 错误原因可能是什么?

    v8::StartupData blob;
v8::SnapshotCreator* creator = NULL;
v8::Isolate* isolate = NULL;
if (g_creator && g_isolate)
{
    creator = g_creator;
    isolate = g_isolate;
}
else
{
    blob.data = (const char*)snapshot.data();
    blob.raw_size = snapshot.size();
    creator = new v8::SnapshotCreator(NULL, &blob);
    isolate = creator->GetIsolate();
    g_creator = creator;
    g_isolate = isolate;
}
{   
    v8::HandleScope handle_scope(isolate);
    v8::TryCatch try_catch(isolate);
    v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
    v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);//Crash!
    v8::Context::Scope context_scope(context);
    creator->SetDefaultContext(context);
    v8::Local<v8::Value> result;
    //...
    v8::Local<v8::String> source =
    v8::String::NewFromUtf8(isolate,
                            methodcall.c_str(),
                            v8::NewStringType::kNormal).ToLocalChecked();

    v8::Local<v8::Script> script;
    if (!v8::Script::Compile(context, source).ToLocal(&script))
    {
        //...
    }
    if (!script->Run(context).ToLocal(&result))
    {
        //...
    }
    else
    {
        //...
    }
}
blob = creator->CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear);
//...

堆栈:

#0  0x00007ffff779bc07 in v8::internal::Snapshot::NewContextFromSnapshot(v8::internal::Isolate*, v8::internal::Handle<v8::internal::JSGlobalProxy>, unsigned long, v8::DeserializeInternalFieldsCallback)
#1  0x00007ffff71e2722 in v8::internal::Genesis::Genesis(v8::internal::Isolate*, v8::internal::MaybeHandle<v8::internal::JSGlobalProxy>, v8::Local<v8::ObjectTemplate>, unsigned long, v8::DeserializeInternalFieldsCallback, v8::internal::GlobalContextType)
#2  0x00007ffff71c92f7 in v8::internal::Bootstrapper::CreateEnvironment(v8::internal::MaybeHandle<v8::internal::JSGlobalProxy>, v8::Local<v8::ObjectTemplate>, v8::ExtensionConfiguration*, unsigned long, v8::DeserializeInternalFieldsCallback, v8::internal::GlobalContextType) ()
#3  0x00007ffff7182c3f in v8::NewContext(v8::Isolate*, v8::ExtensionConfiguration*, v8::MaybeLocal<v8::ObjectTemplate>, v8::MaybeLocal<v8::Value>, unsigned long, v8::DeserializeInternalFieldsCallback) ()
#4  0x00007ffff7163230 in v8::Context::New(v8::Isolate*, v8::ExtensionConfiguration*

0 个答案:

没有答案