I would like to return an object which has its own object template, but am not sure how to do this without allocating memory for that object. This is because I don't know how/when to delete this memory.
Here is what I have so far:
auto objectTemplate = MyObject::GetObjectTemplate(isolate);
auto context = isolate->GetCurrentContext();
auto result = new RespectiveObject();
// set values in `result`...
auto instance = objectTemplate->NewInstance(context).ToLocalChecked();
instance->SetInternalField(0, v8::External::New(isolate, result)); // allocation needed for here so `v8::External::New` can store the object instance
args.GetReturnValue().Set(instance);
I there a way to avoid the allocation, or is there a way to somehow track this allocation and delete it when it is no longer needed? Perhaps the issue is my use of v8::External
itself?
It looks like there is no way to avoid allocating memory, but the memory can be garbage collected by using v8::PersistentBase::SetWeak()
.