I don't know how this worked in v10, but recent versions this is how you
clean up native objects once js objects get collected:
static void weak_callback(const v8::WeakCallbackInfo<GarbageCollected>&
data)
{
data.GetParameter()->handle.Reset(); // Reset the handle that owns the
object, you can't do ANYTHING else here. Effectively each GarbageCollected
object in my case, has a handle to itself, keeping it alive.
data.SetSecondPassCallback([](const v8::WeakCallbackInfo<
GarbageCollected>& data) // Add a callback that gets called later by the
GC, once the GC realises there are no more handles to the object. This is
where actual GC happens.
{
delete data.GetParameter();
});
}
On Tuesday, 10 June 2025 at 08:30:36 UTC+1 [email protected] wrote:
> Hi,
>
> I'm updating v8 from v10.2.154.26 to v 12.4.254.
> I found out kFinalizer is deprecated in the newer version so I have to
> rework the weakcall back part in my implementation.
> When I simply change the kFinalizer in to kParameter in my SetWeak call,
> the program crashes when I calles SetAlignedPointerInInternalField in the
> destructor of my class under certain circumstance.
> I'm wondering what's the correct way to use internal field, and the reason
> why it's crashing if I change WeakCallbackType from kFinalizer to
> kParameter.
>
> The following is some code segment of my implementation.
>
> class A {
> v8::Persistent<v8::Object> mObject;
> }
>
> A::~A ()
> {
> if ( !mObject.IsEmpty() )
> mObject.ClearWeak();
> mObject.Reset();
> }
>
> void
> A::Wrap (v8::Local<v8::Object> const &object)
> {
> object->SetAlignedPointerInInternalField(0, this);
> mObject.Reset(v8::Isolate::GetCurrent(), object);
> }
>
> B::B() :A() {}
>
> B::~B()
> {
> if (!mObject.IsEmpty() && GetRuntime()->IsValid())
> {
> v8::Local<v8::Object> obj =
> v8::Local<v8::Object>::New(v8::Isolate::GetCurrent(), mObject);
> obj->SetAlignedPointerInInternalField(0, NULL); // Crashes here,
> seems obj is already collected, Is this needed to prevent dangling pointer?
> }
> }
>
> C::C()
> :B()
> {
> v8::Local<v8::Object> target =
> Initialize()->GetFunction(ctx).ToLocalChecked()
> ->NewInstance(ctx).ToLocalChecked();
>
> Wrap(target);
> mObject.SetWeak(this, WeakCallback, v8::WeakCallbackType::kParameter);
> // Was kFinalizer in previous version, no crash.
> }
>
> C::~C()
> {
> mObject.ClearWeak();
> }
>
> void
> B::WeakCallback(const v8::WeakCallbackInfo<C>& data)
> {
> C* self = data.GetParameter();
> self->EarlyComplete();
> delete self;
> }
--
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/v8-users/f190670c-6cc9-4d67-878b-23278d5a1e0fn%40googlegroups.com.