donturner
donturner

Reputation: 19146

Why does Hello World for V8 cause a segmentation fault on Ubuntu?

I have compiled V8 on Ubuntu 14.04 and am now trying to get the sample hello_world.cc working, however, when I execute it I get a Segmentation fault (core dumped).

Here's my source for hello_world.cc:

#include <v8.h>

using namespace v8;

int main(int argc, char* argv[]) {
  // Get the default Isolate created at startup.
  Isolate* isolate = Isolate::GetCurrent();

  // Create a stack-allocated handle scope.
  HandleScope handle_scope(isolate);

  return 0;
}

Following the instructions, here's the command I used to build hello_world.cc into an executable:

g++ -Iinclude -g hello_world.cc -o hello_world -Wl,--start-group out/x64.debug/obj.target/{tools/gyp/libv8_{base,snapshot},third_party/icu/libicu{uc,i18n,data}}.a -Wl,--end-group -lrt -lpthread

Note that, in addition to the instructions, I had to add -lpthread flag to get it to compile and -g to include the debug symbols.

Here's the output from the program:

$ ./hello_world
Segmentation fault (core dumped)

And if I run gdb hello_world core I get:

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./hello_world'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004148bb in v8::HandleScope::Initialize (this=0x7fff9b86a110, isolate=0x0) at ../src/api.cc:572
572   prev_next_ = current->next;

Line 572 from src/api.cc is here

Upvotes: 2

Views: 1238

Answers (1)

donturner
donturner

Reputation: 19146

Add a check for Isolate* isolate = Isolate::GetCurrent(); returning NULL:

Isolate* isolate = Isolate::GetCurrent();
if(!isolate) {
    isolate = v8::Isolate::New();
    isolate->Enter();
}

Upvotes: 2

Related Questions