Hi Mads,
Thanks for your reply.
From your hints, I found the following article:
http://create.tpsitulsa.com/blog/2009/01/31/persistent-handles/
The content of that should be the same as your meaning and I have
implemented the following code:
typedef void (*DestructorCallBack)(v8::Handle<v8::Object> object);
struct __DESTRUCTOR_S
{
DestructorCallBack cb;
v8::Persistent<v8::Object> ref;
};
static void __DestructorCallback(v8::Persistent<v8::Value> ref, void*
parameter)
{
v8::HandleScope handle_scope;
__DESTRUCTOR_S *pds = (__DESTRUCTOR_S *)parameter;
pds->cb(v8::Handle<v8::Object>::Cast(ref));
delete pds;
}
void SetDestructor(v8::Handle<v8::Object> self, DestructorCallBack cb)
{
__DESTRUCTOR_S *pds = new __DESTRUCTOR_S();
pds->cb = cb;
pds->ref = v8::Persistent<v8::Object>::New(self);
pds->ref.MakeWeak(pds, __DestructorCallback);
}
And in the constructor, just call " SetDestructor(self, <a callback to
destroy the internal fields>);" to setup a destructor.
In the code, I used a structure to store a callback and a
PersistentHandle and pass it to the Weak Reference callback. In the
generic __DestructorCallback, it calls the callback stored in the
structure and delete the structure. Is it correct? and do I need to
destroy the Persistent Handle explicitly?
Regards,
Thomas