我应该在v8 :: External中手动删除指针吗? [英] Shall I delete the pointer manually in v8::External?

查看:68
本文介绍了我应该在v8 :: External中手动删除指针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Local<ObjectTemplate> tpl = ObjectTemplate::New(isolate);
tpl->SetInternalFieldCount(1);
Local<Object> ret = tpl->NewInstance();
TestExternal* ex = new TestExternal();
ret->SetInternalField(0, External::New(isolate, ex));

ret 不再使用时,我应该手动删除 ex 指针吗?

Shall I delete the ex pointer manually when ret is no longer in use?

证据源代码在哪里显示我应该还是不应该这样做?

Where's the evidence source code shows that I should or shouldn't do that?

推荐答案

是的,C ++需要手动进行内存管理:如果使用 new 手动创建对象,则还必须手动删除它不再需要时.如果不删除它,则程序可以运行,但会泄漏内存.如果您删除它太早(而其他对象仍然具有指向它的指针),则将其称为释放后使用"错误,该错误通常会导致崩溃并可以被利用.

Yes, C++ requires manual memory management: if you manually create an object with new, then you also have to manually delete it when it is no longer needed. If you don't delete it, then your program will work, but it will leak memory. If you delete it too early (while other objects still have pointers to it), then that's called a "use-after-free" bug, which typically causes crashes and can be exploited.

关于V8,没有关于此的特定内容. v8 :: External 无法自动删除您的对象,因为它不知道您的应用程序如何工作-只有知道何时可以安全地删除对象,以及它们必须如何删除被删除( void * 不知道析构函数).

There is nothing specific to V8 about this. The v8::External cannot automatically delete your objects because it does not know how your application works -- only you know when the objects can safely be deleted, and how they must be deleted (a void* doesn't know about destructors).

V8的 Persistent 句柄标记为弱",并在V8的GC即将释放它们所引用的对象时调用回调.但是,v8.h中的文档强烈建议不要依赖于此:

V8's Persistent handles can be marked "weak" and invoke a callback when V8's GC is about to free the object they're referring to. However, the documentation in v8.h strongly recommends not to rely on this:

注意:不能保证何时甚至如果回调是调用.仅在尽力而为的基础上执行该调用.与往常一样,任何情况均不应依赖基于GC的终结处理资源管理的关键形式!

NOTE: There is no guarantee as to when or even if the callback is invoked. The invocation is performed solely on a best effort basis. As always, GC-based finalization should not be relied upon for any critical form of resource management!

因此,您应该在C ++端跟踪所有对象,并制定一些计划B来释放它们.

So you should keep track of all your objects on the C++ side and have some plan B for freeing them.

这篇关于我应该在v8 :: External中手动删除指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆