From 8ef07251ecd090d078607bbcbaa22fee669d2386 Mon Sep 17 00:00:00 2001 From: Nicola Del Gobbo Date: Mon, 22 Mar 2021 21:40:23 +0100 Subject: [PATCH] doc: added some warnings for buffer and array buffer factory method. (#929) * Fixes issue 258. --- doc/array_buffer.md | 6 ++++++ doc/buffer.md | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/doc/array_buffer.md b/doc/array_buffer.md index 346fe6ace..b089b0dfe 100644 --- a/doc/array_buffer.md +++ b/doc/array_buffer.md @@ -29,6 +29,12 @@ The `Napi::ArrayBuffer` instance does not assume ownership for the data and expects it to be valid for the lifetime of the instance. Since the `Napi::ArrayBuffer` is subject to garbage collection this overload is only suitable for data which is static and never needs to be freed. +This factory method will not provide the caller with an opportunity to free the +data when the `Napi::ArrayBuffer` gets garbage-collected. If you need to free +the data retained by the `Napi::ArrayBuffer` object please use other +variants of the `Napi::ArrayBuffer::New` factory method that accept +`Napi::Finalizer`, which is a function that will be invoked when the +`Napi::ArrayBuffer` object has been destroyed. ```cpp static Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env, void* externalData, size_t byteLength); diff --git a/doc/buffer.md b/doc/buffer.md index 97ed48a5a..55e26e90b 100644 --- a/doc/buffer.md +++ b/doc/buffer.md @@ -28,6 +28,12 @@ The `Napi::Buffer` object does not assume ownership for the data and expects it valid for the lifetime of the object. Since the `Napi::Buffer` is subject to garbage collection this overload is only suitable for data which is static and never needs to be freed. +This factory method will not provide the caller with an opportunity to free the +data when the `Napi::Buffer` gets garbage-collected. If you need to free the +data retained by the `Napi::Buffer` object please use other variants of the +`Napi::Buffer::New` factory method that accept `Napi::Finalizer`, which is a +function that will be invoked when the `Napi::Buffer` object has been +destroyed. ```cpp static Napi::Buffer Napi::Buffer::New(napi_env env, T* data, size_t length);