napi_handle_scope napi 内存管理

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

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值