To flesh out Ben's answer a bit, you'd want a Persistent reference to obj which you could SetWeak. I guess maybe that's obvious but never hurts to state the obvious. The Persistent reference could be inside Point or a base class of Point (and others) if you have a menagerie of object types to manage.
If you're concerned about the non-deterministic nature of the cleanup that Ben pointed out, you can make Point (and any other such objects) extend String::ExternalStringResource. Heap tear down will call Dispose for every ExternalString in the Isolate. In addition, if obj is collected, and the presumed only reference to p (the interal field) is eliminated, the ExternalString will be collected and Dispose called for p. If all you want Dispose to do is delete p, you don't have to specify a Dispose override. It might have been nice if V8 had a similar mechanism for plain External objects as it's a bit weird having your objects extend ExternalStringResource but on the grand scheme of weird it's not a big deal. Note that the determistic aspect of String::ExternalStringResource is only really important if your process creates and destroys multiple isolates as a matter of course so you don't want to wait for process termination to clean everything up. Have fun! On Monday, April 19, 2021 at 8:46:33 AM UTC-5 Ben Noordhuis wrote: > On Sun, Apr 18, 2021 at 8:13 PM Richard <richard.j...@gmail.com> wrote: > > > > In the following sample code from: > https://v8.dev/docs/embed#accessing-dynamic-variables > > > > Point* p = ...; > > v8::Local<v8::Object> obj = point_templ->NewInstance(); > > obj->SetInternalField(0, v8::External::New(isolate, p)); > > > > Is there a way to know when `p` has been finished with? I would like to > know when/how to delete the allocated memory. > > See v8::Global::SetWeak(), it lets you register a callback that's > invoked when the object is reclaimed by the garbage collector. > > Caveat emptor, garbage collection is non-deterministic; it might > simply never run with short-lived scripts. > -- -- v8-users mailing list v8-users@googlegroups.com 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 v8-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/v8-users/d1c2b401-dabe-4239-90f4-9926d14bbfd8n%40googlegroups.com.