【问题标题】:A crash on V8' Context::NewV8' Context::New 崩溃
【发布时间】:2019-10-11 12:54:36
【问题描述】:

我围绕 Google V8 引擎实现了一个包装器。我写了一个类:

class Es
{
public:
    Es();
    ~Es();

    int Init(const char* exec_path);

    int CreateContext(uint& id);
    int RemoveContext(const uint id);

protected:
    Global<Context> global_context;
    std::map<uint, Persistent<Context>*> contexts;
    Isolate* isolate = nullptr;

private:
    uint next_id = 1;
};

我想创建上下文,将它们保存在上下文 var 中,并在某一天将其删除。所以,我启动了 V8 引擎:

int Es::Init(const char* exec_path)
{
    v8::V8::InitializeICUDefaultLocation(exec_path);
    v8::V8::InitializeExternalStartupData(exec_path);
    std::unique_ptr<Platform> 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);
    if (!isolate)
        return InitError;

    return Success;
}

然后我想创建一个上下文,使用 int Es::CreateContext(uint& id)。它在 Init 之后调用。

int EasyProspect::CreateContext(uint& id)
{
    if (!isolate)
        return NotInitializedError;

    Isolate::Scope isolate_scope(isolate);
    HandleScope handle_scope(isolate);
    Local<Context> local_context = Context::New(isolate);
    Persistent<Context> context(isolate, local_context);
    contexts.emplace(id, &context);
    return Success;
}

但我做不到,代码在 Context::New(isolate) 上崩溃。为什么?隔离不为空,我进入本地范围...

【问题讨论】:

    标签: v8 embedded-v8


    【解决方案1】:

    最好的办法是在调试模式下编译并在调试器中运行。那么应该很容易判断导致崩溃的原因。

    (至少,您应该发布一个完整的可重现示例,包括指定您正在使用的 V8 版本、它是如何构建/配置的,以及您如何编译代码。)

    如果我不得不猜测:只要您想使用 V8 实例,PlatformArrayBuffer::Allocator 就需要保持活动状态,但在您的代码中,它们都在 Es::Init 的末尾被销毁。由于Es 是一个包装类,您可以轻松地在其中添加字段以保留它们。

    【讨论】:

    • 非常感谢!这是关于平台和分配器的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多