在 Node.js 环境退出时完成
¥Finalization on the exit of the Node.js environment
Node.js 环境可能会在任意时间尽快拆除,不允许 JavaScript 执行,就像 worker.terminate()
的请求一样。当环境被拆除时,JavaScript 对象、线程安全函数和环境实例数据的已注册 napi_finalize
回调将立即独立调用。
¥The Node.js environment may be torn down at an arbitrary time as soon as
possible with JavaScript execution disallowed, like on the request of
worker.terminate()
. When the environment is being torn down, the
registered napi_finalize
callbacks of JavaScript objects, thread-safe
functions and environment instance data are invoked immediately and
independently.
napi_finalize
回调的调用安排在手动注册的清理钩子之后。为了确保在环境关闭期间插件完成的正确顺序以避免 napi_finalize
回调中的释放后使用,插件应该向 napi_add_env_cleanup_hook
和 napi_add_async_cleanup_hook
注册一个清理钩子,以便以正确的顺序手动释放分配的资源。
¥The invocation of napi_finalize
callbacks is scheduled after the manually
registered cleanup hooks. In order to ensure a proper order of addon
finalization during environment shutdown to avoid use-after-free in the
napi_finalize
callback, addons should register a cleanup hook with
napi_add_env_cleanup_hook
and napi_add_async_cleanup_hook
to manually
release the allocated resource in a proper order.