v8 embedded: crash at Isolate::New when trying to initialize again

55 views
Skip to first unread message

Chris Chen

unread,
Apr 10, 2020, 6:47:08 AM4/10/20
to v8-users
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!
 

Ben Noordhuis

unread,
Apr 10, 2020, 11:15:29 AM4/10/20
to v8-users
Call V8::InitializePlatform(), V8::Initialize(), V8::Dispose() and
V8::ShutdownPlatform() only once, not twice.
Message has been deleted

Chris Chen

unread,
Apr 11, 2020, 7:17:18 AM4/11/20
to v8-users
Thanks for your reply, Ben.

In hello-world example, if call V8::xxx() only once, things are alright. 

However, in my embedded program, those initializations are called in seperate thread, and it fails, is that v8 can only do those initializations in only one process?

在 2020年4月10日星期五 UTC+8下午11:15:29,Ben Noordhuis写道:
Reply all
Reply to author
Forward
0 new messages