Node-API memory management types
napi_handle_scope
This is an abstraction used to control and modify the lifetime of objects created within a particular scope. In general, Node-API values are created within the context of a handle scope. When a native method is called from JavaScript, a default handle scope will exist. If the user does not explicitly create a new handle scope, Node-API values will be created in the default handle scope. For any invocations of code outside the execution of a native method (for instance, during a libuv callback invocation), the module is required to create a scope before invoking any functions that can result in the creation of JavaScript values.
Handle scopes are created using napi_open_handle_scope and are destroyed using napi_close_handle_scope. Closing the scope can indicate to the GC that all napi_values created during the lifetime of the handle scope are no longer referenced from the current stack frame.
翻译
这是一个抽象,用于控制和修改在特定范围内创建的对象的生存期。通常,Node-API值是在句柄范围的上下文中创建的。
当从JavaScript调用本机方法时,将存在默认的句柄范围。如果用户没有显式创建新的句柄作用域,则将在默认句柄作用域中创建Node-API值。
对于本机方法执行之外的任何代码调用(例如,在libuv回调调用期间),
模块需要在调用任何可能导致创建JavaScript值的函数之前创建一个范围。
句柄作用域使用napi_open_handl_scope创建,并使用napi_close_handl_scope销毁。
关闭作用域可以向GC指示在句柄作用域的生存期内创建的所有napi_值不再从当前堆栈帧中引用。
- napi_open_handle_scope
NAPI_EXTERN napi_status napi_open_handle_scope(napi_env env,
napi_handle_scope* result);
[in] env: The environment that the API is invoked under.
[out] result: napi_value representing the new scope.
Returns napi

本文介绍了Node-API中的句柄作用域概念,用于管理在特定范围内的对象生命周期。它包括如何创建(napi_open_handle_scope)和销毁(napi_close_handle_scope)句柄作用域,以及在libuv回调中正确使用它们以避免内存泄漏。
744

被折叠的 条评论
为什么被折叠?



