On Mon, May 20, 2019 at 6:59 AM Gautham B A <gautham.bangal...@gmail.com> wrote: > > Hi all, > > I'm trying to track when a variable becomes invalid. > Consider the following code where the variable goes out of scope - > > function SomeFunction() { > for(let i = 0; i < 10; ++i) { > let instance = new MyClass(); // MyClass is implemented in C++ > } > // instance goes out of scope and isn't accessible at this line > } > > Or when a variable gets reassigned. > > function SomeFunction() { > let instance = new MyClass(); // MyClass is implemented in C++ > instance = 10; // instance gets reassigned > } > > Does v8 provide any callbacks that I can register on the object returned by > MyClass which gets called in the above two scenarios? > > The motivation behind my ask is that, When an instance of MyClass is created, > some system resources (such as database connections etc) are allocated and > bound to the instance. I wish to claim them back as soon as we can detect > that the variable's value is unusable, as described in the above two > scenarios. > > Thanks, > --Gautham
If you wrap the object in a v8::Persistent<v8::Object> and then call persistent_handle.SetWeak(...), the garbage collector will invoke your callback when the object is reclaimed. That isn't exactly what you're asking for but it's the closest thing. What you want doesn't exist and cannot exist because it would be prohibitively expensive. Static escape analysis is easily defeated in JS. You effectively would have to perform a full GC after every statement. -- -- 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/CAHQurc-71onVW4_sKRB3c3g%2BcL8phJeZ_8NShg3t%3DgPRcexzBw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.