library.close()


关闭库句柄。

🌐 Closes the library handle.

DynamicLibrary 实现了显式资源管理协议,因此可以使用 using 声明来管理库实例。离开封闭作用域会自动调用 library.close()

import { DynamicLibrary } from 'node:ffi';

{
  using lib = new DynamicLibrary('./mylib.so');
  // Use `lib` here; `lib.close()` is called when the block exits.
} 

多次调用 library.close()(或释放库)不会有任何效果。

🌐 Calling library.close() (or disposing the library) more than once is a no-op.

在库关闭之后:

🌐 After a library has been closed:

  • 已解析的函数封装器失效。
  • 进一步的符号和函数解析会抛出异常。
  • 已注册的回调被作废。

关闭一个库并不会使以前导出的回调指针可以安全重用。Node.js 不会跟踪或撤销已经交给本地代码的回调指针。

🌐 Closing a library does not make previously exported callback pointers safe to reuse. Node.js does not track or revoke callback pointers that have already been handed to native code.

如果本地代码在 library.close() 之后或在 library.unregisterCallback(pointer) 之后仍然持有回调指针,调用该指针将具有未定义行为,这是不允许的,也是危险的:它可能导致进程崩溃、产生错误输出或损坏内存。本地代码必须在库关闭之前或回调注销之前停止使用回调地址。

🌐 If native code still holds a callback pointer after library.close() or after library.unregisterCallback(pointer), invoking that pointer has undefined behavior, is not allowed, and is dangerous: it can crash the process, produce incorrect output, or corrupt memory. Native code must stop using callback addresses before the library is closed or before the callback is unregistered.

从库的某个活动回调中调用 library.close() 是不支持且危险的。回调必须在库关闭之前返回。

🌐 Calling library.close() from one of the library's active callbacks is unsupported and dangerous. The callback must return before the library is closed.