V8 崩溃 Context::New
A crash on V8' Context::New
我围绕 Google V8 引擎实现了一个包装器。我写了一个class:
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) 上崩溃。为什么? isolate 不为空,我进入局部作用域...
最好的办法是在调试模式下编译,运行 在调试器中编译。那么应该很容易判断是什么导致了崩溃。
(至少,您应该 post 一个完整的可重现示例,包括指定您正在使用的 V8 版本,它是如何 built/configured,以及您如何编译代码.)
如果我不得不猜测:只要您想使用 V8 实例,Platform
和 ArrayBuffer::Allocator
就需要保持活动状态,但在您的代码中它们都在Es::Init
结束。由于 Es
是一个包装器 class,您可以轻松地在其中添加字段以保留它们。
我围绕 Google V8 引擎实现了一个包装器。我写了一个class:
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) 上崩溃。为什么? isolate 不为空,我进入局部作用域...
最好的办法是在调试模式下编译,运行 在调试器中编译。那么应该很容易判断是什么导致了崩溃。
(至少,您应该 post 一个完整的可重现示例,包括指定您正在使用的 V8 版本,它是如何 built/configured,以及您如何编译代码.)
如果我不得不猜测:只要您想使用 V8 实例,Platform
和 ArrayBuffer::Allocator
就需要保持活动状态,但在您的代码中它们都在Es::Init
结束。由于 Es
是一个包装器 class,您可以轻松地在其中添加字段以保留它们。