Closed jcmonnin closed 7 years ago
Thanks, this is definitely a bug, I'll fix it.
I'm trying to merge this fix with a shared_ptr
branch. This branch allows to use store wrapped objects either as std::shared_ptr
, or as raw pointers.
Currently there is a workaround to use class_<T>::unreference_external(isolate, ptr)
function for all variables referenced in v8pp
with v8pp::class_<T>::reference_external()
function:
#include "v8.h"
#include "libplatform/libplatform.h"
#include "v8pp/context.hpp"
#include "v8pp/class.hpp"
#include "v8pp/object.hpp"
struct X
{
bool b;
X(bool b) : b(b) {}
};
int main()
{
X x(true);
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();
{
v8pp::context context; // a wrapper for v8::Context
v8::Isolate* isolate = context.isolate();
v8::HandleScope scope(isolate);
v8pp::class_<X> X_class(isolate);
X_class.set("b", &X::b);
context.set("X", X_class);
v8pp::set_option(isolate, isolate->GetCurrentContext()->Global(),
"x", X_class.reference_external(isolate, &x));
bool const b = v8pp::from_v8<bool>(isolate, context.run_script("x.b"));
assert(b == x.b);
// unreference x object before context cleanup
X_class.unreference_external(isolate, &x);
// v8pp::context::~context() calls v8pp::cleanup()
}
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
}
Thanks. The workaround is ok for now. Good to hear you plan to merge the shared_ptr branch to master. I'm currently on that branch. I need std::shared_ptr
.
Fixed in #60
I have a stack allocated object. I wan't to access to that particular instance from JavaScript. I don't want v8 to manage the lifetime of that object. I'm using
v8pp::class_<X>::reference_external
to achieve that. However, when callingv8pp::cleanup(isolate)
v8pp tries to delete the instance. I think this is a bug (unless I'm misunderstandingreference_external
).Please check the code below that illustrates the problem. There is also the AddressSanitizer output below. Sample is a bit long because there is the whole boilerplate code to init/destroy v8, which is relevant for that issue (based on https://chromium.googlesource.com/v8/v8/+/master/samples/hello-world.cc):