Do i need to delete pointer which was passed into Napi::ArrayBuffer::New()

541 Views Asked by At

I am writing a Node Addon Function That returns a Promise, I have code like this.

size_t outbufsize = 1000;
context->data.size = outbufsize;
context->data.data = new char[outbufsize];

When I finish work.

context->deffered.Resolve(Napi::ArrayBuffer::New(env, context->data.data, context->data.size));

Do I need to delete pointer "context->data.data"? if I delete this pointer. I get an undefine behavior ArrayBuffer on the Javascript side. But if I don't delete the pointer, I don't know if the memory is freed or not?

1

There are 1 best solutions below

0
On

I think I should return the Buffer instead of ArrayBuffer.

 context->deffered.Resolve(
         Napi::Buffer<char>::New(
         env,
         context->data.data,
         context->data.size,
         [](Napi::Env env, void *data)
         {
           free(data);
         }));

as the document say---that accept Napi::Finalizer, which is a function that will be invoked when the Napi::Buffer object has been destroyed.