当关联的Javascript对象在V8中被垃圾收集时,如何释放包装的C ++对象? [英] How do you free a wrapped C++ object when associated Javascript object is garbage collected in V8?
问题描述
V8的文档说明了如何创建包装C ++对象的Javascript对象。 Javascript对象持有指向C ++对象实例的指针。我的问题是,让我们说你在堆上创建C ++对象,当你的gc收集Javascript对象时如何获得通知,所以你可以释放堆分配的C ++对象?
<诀窍是创建一个
Persistent
句柄(第二个来自链接的API引用的参考: 持久化
句柄不保留在堆栈上,只有当你特别删除它们时才被删除...当你需要保持一个对象的引用更多比如一个函数调用,或者当句柄的生存时间不对应于C ++范围),并调用 MakeWeak()
,传递一个回调函数, (当一个对象的唯一引用来自弱持久句柄时,使用 Persistent :: MakeWeak
可以使永久句柄变弱,从垃圾回收器触发回调。 - 即所有常规句柄超出范围,当垃圾收集器要删除对象时)。 Persistent :: MakeWeak
方法签名是:
void MakeWeak(void * parameters,WeakReferenceCallback callback );
其中 WeakReferenceCallback
具有两个参数:
typedef void(* WeakReferenceCallback)(Persistent< Object&参数);
这些都是在V8作为公共API分发的头文件中找到的。
您需要传递给 MakeWeak
的函数来清除 Persistent< Object& code>对象参数,当它被调用作为回调时将被传递给它。可以忽略
void * parameter
参数(或 void * parameter
可以指向包含对象的C ++结构需要清除):
void CleanupV8Point(Persistent< Object> object,void *)
{
//对任何你想要的对象清理
object.destroyCppObjects();
}
参数< ObjectTemplate> my_obj(ObjectTemplate :: New());
//当my_obj的Javascript部分即将被收集时
//我们将有V8调用CleanupV8Point(my_obj)
my_obj.MakeWeak(NULL,& CleanupV8Point );
V8's documentation explains how to create a Javascript object that wraps a C++ object. The Javascript object holds on to a pointer to a C++ object instance. My question is, let's say you create the C++ object on the heap, how can you get a notification when the Javascript object is collected by the gc, so you can free the heap allocated C++ object?
The trick is to create a Persistent
handle (second bullet point from the linked-to API reference: "Persistent
handles are not held on a stack and are deleted only when you specifically remove them. ... Use a persistent handle when you need to keep a reference to an object for more than one function call, or when handle lifetimes do not correspond to C++ scopes."), and call MakeWeak()
on it, passing a callback function that will do the necessary cleanup ("A persistent handle can be made weak, using Persistent::MakeWeak
, to trigger a callback from the garbage collector when the only references to an object are from weak persistent handles." -- that is, when all "regular" handles have gone out of scope and when the garbage collector is about to delete the object).
The Persistent::MakeWeak
method signature is:
void MakeWeak(void* parameters, WeakReferenceCallback callback);
Where WeakReferenceCallback
is defined as a pointer-to-function taking two parameters:
typedef void (*WeakReferenceCallback)(Persistent<Object> object,
void* parameter);
These are found in the v8.h header file distributed with V8 as the public API.
You would want the function you pass to MakeWeak
to clean up the Persistent<Object>
object parameter that will get passed to it when it's called as a callback. The void* parameter
parameter can be ignored (or the void* parameter
can point to a C++ structure that holds the objects that need cleaning up):
void CleanupV8Point(Persistent<Object> object, void*)
{
// do whatever cleanup on object that you're looking for
object.destroyCppObjects();
}
Parameter<ObjectTemplate> my_obj(ObjectTemplate::New());
// when the Javascript part of my_obj is about to be collected
// we'll have V8 call CleanupV8Point(my_obj)
my_obj.MakeWeak(NULL, &CleanupV8Point);
这篇关于当关联的Javascript对象在V8中被垃圾收集时,如何释放包装的C ++对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!