On Thu, Nov 16, 2017 at 1:24 AM, Jean-Marc Le Roux <[email protected] > wrote:
> I assume you're storing references to the objects as Persistent? > > > Nope. You've got the whole code up there. > So if I understand correctly: > > - the objects I need to keep track of the lifecycle should be made > Persistent ; > - all the other values can be set as Local ; > - I should set the weak callback using MakeWeak ; > - callback will be called when such object is GCed and I can free my > C/C++ memory then. > > Is that correct ? > Yes. I sthis perhaps related to Node? Node offers a C++ Clss extention ObjectWrap https://nodejs.org/api/addons.html#addons_wrapping_c_objects https://github.com/nodejs/node/blob/master/src/node_object_wrap.h which simplifies that whole process... when the object is GC'd the class destructor will get called so you can release values there. > > 2017-11-15 19:50 GMT+01:00 J Decker <[email protected]>: > >> I assume you're storing references to the objects as Persistent? All you >> need to do then is call SetWeak() https://v8docs.nodes >> ource.com/node-0.10/d2/d78/classv8_1_1_persistent.html >> void MakeWeak (void *parameters, WeakReferenceCallback callback) >> >> the callback is called when the object is GC'd. >> >> >> On Wed, Nov 15, 2017 at 8:37 AM, Jean-Marc Le Roux < >> [email protected]> wrote: >> >>> So I've found a solution to make things lazy: I set an accessor instead >>> of decoding/setting the actual value. >>> >>> void >>> getter(Local<Name> property, const PropertyCallbackInfo<Value>& info) >>> { >>> Isolate* isolate = info.GetIsolate(); >>> >>> const bson_t* document = reinterpret_cast<bson_t*>( >>> info.This() >>> ->Get(String::NewFromUtf8(isolate, "__bson_document_ptr")) >>> ->ToInt32()->Value() >>> ); >>> >>> char key[255]; >>> property->ToString(isolate)->WriteUtf8(key, 255); >>> >>> bson_iter_t iter; >>> bson_iter_init(&iter, document); >>> // FIXME: index the property so we don't have to find it >>> bson_iter_find(&iter, key); >>> >>> // FIXME: replace the accessor with the deserialized value >>> >>> info.GetReturnValue().Set(iterator_to_value(isolate, &iter)); >>> } >>> >>> Local<Value> >>> fill_object(Isolate* isolate, Local<Object>& obj, const bson_t* document) >>> { >>> obj->Set( >>> String::NewFromUtf8(isolate, "__bson_document_ptr"), >>> Int32::New(isolate, reinterpret_cast<intptr_t>(document)) >>> ); >>> >>> bson_iter_t iter; >>> if (bson_iter_init(&iter, document)) >>> { >>> while (bson_iter_next(&iter)) >>> { >>> const char* key = bson_iter_key(&iter); >>> >>> if (!obj->Has(String::NewFromUtf8(isolate, >>> key))) >>> { >>> obj->SetAccessor( >>> isolate->GetCurrentContext(), >>> String::NewFromUtf8(isolate, key), >>> &getter >>> ); >>> } >>> } >>> } >>> } >>> >>> The secret sauce is to : >>> >>> - keep the original data (the bson_t) allocated >>> - store the corresponding pointer in a v8::Object >>> >>> But now I have a memory leak because those pointers will never be freed. >>> How can I know when the Object will be disposed by the GC? >>> >>> Thanks, >>> >>> -- >>> -- >>> v8-users mailing list >>> [email protected] >>> http://groups.google.com/group/v8-users >>> --- >>> You received this message because you are subscribed to the Google >>> Groups "v8-users" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- >> -- >> v8-users mailing list >> [email protected] >> http://groups.google.com/group/v8-users >> --- >> You received this message because you are subscribed to a topic in the >> Google Groups "v8-users" group. >> To unsubscribe from this topic, visit https://groups.google.com/d/to >> pic/v8-users/aVeevQcHJ2c/unsubscribe. >> To unsubscribe from this group and all its topics, send an email to >> [email protected]. >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > *Jean-Marc Le Roux* > > > Founder and CEO of Aerys (http://aerys.in) > > Blog: http://blogs.aerys.in/jeanmarc-leroux > Cell: (+33)6 20 56 45 78 <+33%206%2020%2056%2045%2078> > Phone: (+33)9 72 40 17 58 <+33%209%2072%2040%2017%2058> > > -- > -- > v8-users mailing list > [email protected] > http://groups.google.com/group/v8-users > --- > You received this message because you are subscribed to the Google Groups > "v8-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
