On Sat, May 16, 2009 at 1:27 PM, Mike Belshe <mbel...@google.com> wrote:

> You can create a weak reference to the object, and when the object is GC'd
> in v8, the weak reference handler will get called.  Check out the v8.h
> documentation for weak references and it will probably make sense.
> Mike
>

Hi Mike,

So I guess the code would look like this:

void MyDestructor(Persistent<Object> object, void*)
{
  // do whatever cleanup is required
}

static v8::Handle<v8::Value> MyCallback(const v8::Arguments& args) {
  // Create a new V8 object template with one internal field.
  v8::Handle<v8::ObjectTemplate> my_templ = v8::ObjectTemplate::New();
  my_templ->SetInternalFieldCount(1);

  // Create a new MyClass instance.
  MyClass* my_class = ...;

  // Create a new persistent V8 object.
  v8::Persistent<v8::Object> obj =
v8::Persistent<v8::Object>::New(my_templ->NewInstance());

  // Attach the MyClass instance to the V8 object.
  obj->SetInternalField(0, v8::External::New(my_class));

  // Make the V8 object a weak reference and assign the destructor.
  obj.MakeWeak(NULL, &MyDestructor);

  return obj;
}

In what circumstances, if any, would it be appropriate to make the
ObjectTemplate a weak reference instead of the Object?  Would any Objects
created with a weak reference ObjectTemplate automatically have the
destructor called for them?

Thanks,
Marshall


>
>
> On Fri, May 15, 2009 at 8:38 PM, Marshall Greenblatt <
> magreenbl...@gmail.com> wrote:
>
>> Hi All,
>>
>> I'm seeking a way to trigger destruction (or reference decrement) of a C++
>> object attached to a V8 object when the V8 object is destroyed.  For
>> instance, consider the following code where I create and return a new V8
>> object in a function template callback:
>>
>> static v8::Handle<v8::Value> MyCallback(const v8::Arguments& args) {
>>   // Create a new V8 object template with one internal field.
>>   v8::Handle<v8::ObjectTemplate> my_templ = v8::ObjectTemplate::New();
>>   my_templ->SetInternalFieldCount(1);
>>
>>   // Create a new MyClass instance.
>>   MyClass* my_class = ...;
>>
>>   // Create a new V8 object.
>>   v8::Local<v8::Object> obj = my_templ->NewInstance();
>>
>>   // Attach the MyClass instance to the V8 object.
>>   obj->SetInternalField(0, v8::External::New(my_class));
>>
>>   return obj;
>> }
>>
>> How can I clean up the MyClass instance when the associated V8 object
>> destroyed?  Is there some way that I can assign a destructor callback for
>> the V8 object?
>>
>> Thanks,
>> Marshall
>>
>> >>
>>
>

--~--~---------~--~----~------------~-------~--~----~
Chromium Developers mailing list: chromium-dev@googlegroups.com 
View archives, change email options, or unsubscribe: 
    http://groups.google.com/group/chromium-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to