Skip to content

External data lifetime for Buffer and ArrayBuffer #258

Closed
@kfarnung

Description

@kfarnung

While writing up the documentation for Buffer and ArrayBuffer I came across some suspicious looking factory methods:

static ArrayBuffer New(napi_env env, void* externalData, size_t byteLength);
static Buffer<T> New(napi_env env, T* data, size_t length);

Digging through the code a bit, it appears that these methods take in a pointer to data and then create an external wrapper object with no finalizer.

Looking at the NAN documentation for buffers it seems like the behavior is to take ownership of the memory and then later free it. Even NAN's approach is arguably dangerous since it makes assumptions about how and where the memory was allocated.

Since node-addon-api doesn't actually free the data and doesn't provide any way for the user to free the data, the only use case I can see for this API is for data which has a static lifetime.

Given their limited (and dangerous) usage, maybe we should consider removing these overloads?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions