2

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

donturner
  • 17,867
  • 8
  • 59
  • 81
  • Have you tried running the *whole* example as opposed to just the first quarter? – Shoe May 23 '14 at 07:59
  • Yes, it segfaults at the same line. I removed everything after it to avoid posting code which wasn't relevant to the problem. – donturner May 23 '14 at 08:04
  • 1
    the gdb trace tells you what is wrong, isolate is a null pointer, so current will probably be a garbage address as well, look into Isolate::GetCurrent to see why you get a null pointer – Radu Chivu May 23 '14 at 08:09
  • Thanks, that's definitely the cause, though a solution still eludes me. I've asked a new question about this here: http://stackoverflow.com/questions/23825034/in-v8-why-does-isolategetcurrent-return-null – donturner May 23 '14 at 09:03
  • I ran into this issue earlier today when using a snapshot `snapshot_blob.bin` from a previous v8 installation, it is possible the snapshot was corrupted or another less obvious issue, but it's a clue for anyone else running into this issue. – wizebin Dec 31 '17 at 06:05

1 Answers1

2

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

Isolate* isolate = Isolate::GetCurrent();
if(!isolate) {
    isolate = v8::Isolate::New();
    isolate->Enter();
}
donturner
  • 17,867
  • 8
  • 59
  • 81