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
source
share