Search code examples
c++v8

Segmentation fault in v8 hello world when using TryCatch


I am trying to use v8 in my C++ application. I am stuck on the helloworld itself!

The helloworld at https://developers.google.com/v8/get_started works just fine. Now I am trying to catch exceptions/error in the code. So I used TryCatch trycatch;.

int main(int argc, char *argv[]) {
    HandleScope handle_scope;
    Persistent<Context> context = Context::New();
    Context::Scope context_scope(context);
    TryCatch trycatch; /* TO CATCH EXCETIONS/ERRORS */
    Handle<String> source = String::New("xyz();");
    Handle<Script> script = Script::Compile(source);
    Handle<Value> result = script->Run();
    if (result.IsEmpty()) {
        fprintf(stderr, "Exception: %s\n",
                *String::AsciiValue(trycatch.Exception()));
        return -1;
    }
    String::AsciiValue ascii(result);
    printf("%s\n", *ascii);
    context.Dispose();

    return 0;
}

The exceptions are caught fine but the program does not terminate properly. It generates a segmentation fault. What am I doing wrong?


Solution

  • Turned out to be a foolish thing. I had long back installed libv8-dev and forgotten about it. And now I installed V8 from source. So I had two versions of V8 on my system. I uninstalled libv8-dev and the problem has been solved.