Hi, all, I am embedding v8 into a c++ program, and when trying to initialize an Isolate again, the program crashes.
The way I was doing is made an v8::Isolate as a member field of an JsEngine instance, then call ~JsEngine() and renew a JsEngine instance. In ~JsEngine(), I do stuff like disposed Isolate, shutdown platform, etc.
This can be reproduced by some modification to samples/hello-world.cc as following:
int main(int argc, char* argv[]) {
// Initialize V8.
v8::V8::InitializeICUDefaultLocation(argv[0]);
v8::V8::InitializeExternalStartupData(argv[0]);
std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
// Create a new Isolate and make it the current one.
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);
{
v8::Isolate::Scope isolate_scope(isolate);
// -------------------------------------------------
// ignore. evaluate some js strings here.
// --------------------------------------------------
// Dispose the isolate and tear down V8.
isolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete create_params.array_buffer_allocator;
// --------------------------------------------------
// do it again for testing
// --------------------------------------------------
v8::V8::InitializeICUDefaultLocation(argv[0]);
v8::V8::InitializeExternalStartupData(argv[0]);
std::unique_ptr<v8::Platform> platform2 = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform2.get());
v8::V8::Initialize();
// Create a new Isolate and make it the current one.
v8::Isolate::CreateParams create_params2;
create_params2.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
v8::Isolate* isolate2 = v8::Isolate::New(create_params2); // crash here.
return 0;
}
The output is like this:
Hello, World!
3 + 4 = 7
Segmentation fault (core dumped)
The backtrace looks like this:
#0 __GI___pthread_mutex_lock (mutex=0x128) at ../nptl/pthread_mutex_lock.c:67
#1 0x00000000007f2073 in v8::internal::wasm::WasmEngine::AddIsolate(v8::internal::Isolate*) ()
#2 0x00000000004cadd8 in v8::internal::Isolate::Init(v8::internal::ReadOnlyDeserializer*, v8::internal::StartupDeserializer*) ()
#3 0x00000000004cb2b9 in v8::internal::Isolate::InitWithSnapshot(v8::internal::ReadOnlyDeserializer*, v8::internal::StartupDeserializer*) ()
#4 0x00000000007d4735 in v8::internal::Snapshot::Initialize(v8::internal::Isolate*) ()
#5 0x000000000044abf2 in v8::Isolate::Initialize(v8::Isolate*, v8::Isolate::CreateParams const&) ()
#6 0x000000000044ad42 in v8::Isolate::New(v8::Isolate::CreateParams const&) ()
#7 0x0000000000414a4e in main ()
(gdb) q
Why this behavior will crash, and how to avoid this?
Any hints would be great, Thanks in advance!