On Wed, Mar 21, 2012 at 03:21, Matt Gollob <mattgol...@gmail.com> wrote: > Hello everyone - > > I'd like some feedback with regard to a sample addon I've put together for > node to demonstrate exposing a C++ library to javascript. In particular, > I'm struggling with how to properly manage memory allocated from C++ when > its lifetime is coupled to the lifetime of a javascript handle. > > The repository is on Github at https://github.com/mattness/addonSample. > Specifically, I'm looking for help with the following: > > https://github.com/mattness/addonSample/blob/master/src/binding.cc#L53 > (Freeing a C++ heap-allocated object when JS object it's tied to gets GC'd) > https://github.com/mattness/addonSample/blob/master/src/native_object_wrapper.cc#L23 > (Whether or not a v8::ObjectTemplate can be defined once and reused) > https://github.com/mattness/addonSample/blob/master/src/native_object_wrapper.cc#L38 > (The appropriate use of Persistent handles, and when/where to call Dispose > and Clear) > > Any other feedback is welcome as well. Thanks so much for your time! > > Matt
Take a look at how node::ObjectWrap does it. Basically, you stuff your object in a Persistent handle and call MakeWeak on it. It's your WeakReferenceCallback that cleans up the C++ resources. We also call MarkIndependent but that's an optimization: marking a handle independent means the garbage collector can make some assumptions that (hopefully) speed up sweeps. -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en