Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External data lifetime for Buffer and ArrayBuffer #258

Closed
kfarnung opened this issue May 3, 2018 · 6 comments · Fixed by #929
Closed

External data lifetime for Buffer and ArrayBuffer #258

kfarnung opened this issue May 3, 2018 · 6 comments · Fixed by #929
Labels

Comments

@kfarnung
Copy link
Contributor

kfarnung commented May 3, 2018

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?

@kfarnung
Copy link
Contributor Author

kfarnung commented May 3, 2018

Per discussion today investigate mimicking the NAN behavior.

@github-actions
Copy link
Contributor

This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.

@mhdawson
Copy link
Member

@NickNaso will review and lead the discussion in a future team meeting.

@mhdawson
Copy link
Member

Touched on in the meeting today, no update, but @NickNaso still looking at it.

@NickNaso
Copy link
Member

Hi everybody, I did not have the time to work on issue until today. I'm starting work on this now, I will update at next Node-API meeting.

@gabrielschulhof
Copy link
Contributor

@NickNaso will modify the Buffer and ArrayBuffer documentation to add a warning for the ::New() variants that do not accept a finalizer about the fact that Buffers and ArrayBuffers created in such a way will not provide the caller with an opportunity to free the data when they get garbage-collected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants