Closed metalicjames closed 7 years ago
I have found the source of the problem. It is important for all v8pp objects to be deconstructed before calling v8::V8::Dispose();
otherwise there will be a segmentation fault. Putting the main bulk of the code in a try-catch clause fixed the problem.
Hi @metalicjames
Yes, you are absolutely right, all V8
and v8pp
objects should be destroyed before v8::V8::Dispose()
call.
I think here in your code should be a scope block for v8pp::context
object. There is also a v8::HandleScope
object in the code, but it is declared later, therefore it will be destroyed earlier than v8pp::context
:
#include <iostream>
#include <v8.h>
#include <libplatform/libplatform.h>
#include <v8pp/context.hpp>
int main(int argc, char* argv[])
{
v8::V8::InitializeICU();
v8::V8::InitializeExternalStartupData(argv[0]);
std::unique_ptr<v8::Platform> platform(v8::platform::CreateDefaultPlatform());
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
// scope block for v8pp::context
{
v8pp::context context;
const std::string source = "2 + 6";
v8::Isolate* isolate = context.isolate();
v8::HandleScope scope(isolate);
v8::TryCatch tryCatch;
v8::Handle<v8::Value> result = context.run_script(source);
if (tryCatch.HasCaught())
{
const std::string msg = v8pp::from_v8<std::string>(isolate, tryCatch.Exception()->ToString());
throw std::runtime_error(msg);
}
const int scriptResult = v8pp::from_v8<int>(isolate, result);
std::cout << scriptResult;
}
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
return 0;
}
Hi,
When trying to execute the following code:
The program executes correctly and scriptResult contains 8 as expected. However, there is a segmentation fault when the deconstructor is called on
v8pp::context context;
with the following backtrace:Am I doing something wrong or is this a bug?