On Sat, May 16, 2009 at 12:10 PM, Marshall Greenblatt < magreenbl...@gmail.com> wrote:
> 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; > } > Yes, that code looks about right. I'd recommend testing it out in test programs so you can see it all in action. > 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? > In general, don't go too crazy with Weak handles. Use it when you need it. Cases where you need it include: a) You're churning through lots of large C++ objects, causing memory usage to skyrocket because V8 doesn't realize the C++ side is holding onto so much memory b) You're holding onto some system resource (like a file, lock, etc) that you want to clean up quickly. I can't imagine cases where you'd want the ObjectTemplate itself to be weak much - because I can't envision a case where (a) or (b) above is relevant to the ObjectTemplate. But maybe I lack vision :-) Mike > > > 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 -~----------~----~----~----~------~----~------~--~---