最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In V8, how would I remove wrapped C++ objects after their JavaScript counterparts are garbage-collected? - Stack Overflow

programmeradmin2浏览0评论

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

How would I modify this so that the Point C++ object that is created has its destructor called and is deleted from memory when the GC for V8 destroys the JavaScript wrapper?

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

How would I modify this so that the Point C++ object that is created has its destructor called and is deleted from memory when the GC for V8 destroys the JavaScript wrapper?

Share Improve this question edited Feb 10, 2013 at 1:13 user1479055 asked Jan 1, 2011 at 7:20 HFLWHFLW 4344 silver badges13 bronze badges 1
  • 1 possible duplicate of How do you free a wrapped C++ object when associated Javascript object is garbage collected in V8? – Qantas 94 Heavy Commented Jul 23, 2015 at 23:45
Add a ment  | 

1 Answer 1

Reset to default 9

You want to create a Persistent handle and make it weak (v8::Persistent::MakeWeak(data, cb)). In the callback you can delete the C++ object. As usual with a garbage collector, the exact time at which weak-reachability will be determined is dependent on when GC is performed. Native resources may therefore be freed much later than you expect. You can inform V8 about the amount of native resources you are holding (v8::AdjustAmountOfExternalAllocatedMemory).

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论