In V8, how can I remove wrapped C ++ objects after their JavaScript collections are garbage collected?

Let's say I have the code presented in this tutorial .

How can I change this so that the created PointC ++ object calls its destructor and is deleted from memory when the GC for V8 breaks the JavaScript shell?

+5
source share
1 answer

You want to create a persistent descriptor and make it weak (v8 :: Persistent :: MakeWeak (data, cb)). In the callback, you can delete the C ++ object. As usual, with the garbage collector, the exact time during which poor availability is determined depends on when the GC runs. Therefore, native resources can be released much later than you expect. You can tell V8 about the amount of your own resources that you are holding (v8 :: AdjustAmountOfExternalAllocatedMemory).

node "ObjectWrap" encapsulates bidirectional object / JS mapping and weak callback: https://github.com/ry/node/blob/master/src/node_object_wrap.h

+6
source

All Articles