v8 FunctionTemplate :: GetFunction()因访问冲突而崩溃

时间:2018-11-21 21:57:34

标签: c++ v8 embedded-v8

我有一个非常简单的方法来初始化v8,我无法跟踪原因(其他v8东西也可以正常工作):

void JavascriptEngine::init() {
    m_platform = v8::platform::NewDefaultPlatform();

    v8::V8::InitializeExternalStartupData(".");

    v8::V8::InitializeICU();
    v8::V8::InitializePlatform(&*m_platform);
    v8::V8::Initialize();

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

    v8::Isolate::Scope isolate_scope(m_isolate);
    v8::HandleScope handle_scope(m_isolate);

    m_context = make_unique<v8::Persistent<v8::Context>>(m_isolate, 
        create_new_context());

    v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(m_isolate);
    t->SetClassName(v8::String::NewFromUtf8(m_isolate, "Test", v8::NewStringType::kNormal).ToLocalChecked());
    t->Set(m_isolate, "func_property", v8::Number::New(m_isolate, 1));

    auto f = t->GetFunction(); // CRASH

    ...//rest of the code
}

我得到的错误:

Exception thrown: read access violation.
**this** was nullptr.

使用Visual Studio2017。我缺少什么?

1 个答案:

答案 0 :(得分:1)

要访问indexpath.row == 0,您需要输入GetFunction并输入。根据您使用的v8版本,有些方法要求显式传递ContextIsolate。通常,这是v8内部移动到的-将ContextIsolate传递给需要此方法的方法(不过可能会有些不一致)。