pmed / v8pp

Bind C++ functions and classes into V8 JavaScript engine
http://pmed.github.io/v8pp/
Other
887 stars 119 forks source link

Duplicate object #156

Closed andyremi closed 2 years ago

andyremi commented 3 years ago

Hello, I get an error like following when I give V8 a wrapped object:

:4012: Uncaught v8pp::class_ duplicate object 0x60000381cd20 Do you have any idea of this message and how to resolve it? Thanks.
andyremi commented 3 years ago

I've resolved this issue but there is another issue. Since I was wrapping a same pointer for two different v8::Object instances, above message was occurred. To resolve that issue, I have to use v8pp::class_::unreference_external manually by calling like jsObject.unwrap() because it seems V8 doesn't destroy the JS object on time even though the local block in JS is finished. I tried to call global.gc(), but it still doesn't call 'destroyed' handler as following:

static void destroyed(const v8::WeakCallbackInfo &args) { MyClass* ptr = v8pp::class_::unwrapobject(iso, args[0]); v8pp::class::unreference_external(engine->iso, ptr); }

Local init() { ... auto wrapped = v8pp::class_::reference_external(iso, myClassObj); auto jsObj = v8::Persistent<v8::Object, v8::CopyablePersistentTraits>(isolate, wrapped); jsObj.SetWeak(NULL, destroyed, v8::WeakCallbackType::kFinalizer); ... return jsObj; }

Is there a way to make a JS variable destroyed automatically & immediately when the local block in JS is finished so that above 'destroyed' function can be called?

pmed commented 3 years ago

Hi,

Is there a way to make a JS variable destroyed automatically & immediately when the local block in JS is finished so that above 'destroyed' function can be called?

I don't know such a way. V8 manages objects lifetime in JavaScript. Wrapped C++ objects that were created for a v8pp::class_<SomeMy> will be occasionally garbage collected by the V8, unless you have manually v8pp::class_::unreference() for a wrapped C++ object.