0

我围绕 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;
};

我想创建上下文,将它们保存在上下文变量中,并在某一天删除它们。因此,我启动了 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) 上崩溃。为什么?隔离不为空,我进入本地范围...

4

1 回答 1

2

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

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

如果我不得不猜测:只要您想使用 V8 实例,就需要保持活动状态,但是在您的代码中,它们都Platform在. 由于它是一个包装类,因此您可以轻松地在其中添加字段以保留它们。ArrayBuffer::AllocatorEs::InitEs

于 2019-10-11T13:46:28.393 回答