您是否必须触发销毁对使用 Python-CFFI 中的 ffi.gc() 创建的变量的最后引用?

Do you have to trigger the destruction of the last reference to a variable created using ffi.gc() from Python-CFFI?

读入Python CFFI documentation

The interface is based on LuaJIT’s FFI (...)

阅读 LuaJIT website(关于 ffi.gc()):

This function allows safe integration of unmanaged resources into the automatic memory management of the LuaJIT garbage collector. Typical usage:

local p = ffi.gc(ffi.C.malloc(n), ffi.C.free)
...
p = nil -- Last reference to p is gone.
-- GC will eventually run finalizer: ffi.C.free(p)

那么,使用 Python-CFFI,您是否必须触发对使用 ffi.gc 实例化的变量的最后引用的销毁(=需要一个特殊的函数来释放,因为某些部分它是动态分配的)通过将其设置为(即)ffi.NULL ?

Python 旨在让所有对象在不再引用它时(或不久之后)被垃圾收集,就像任何其他垃圾收集语言(包括 Lua)一样.显式设置 p = None(或 del p)的技巧只会确保此局部变量 p 不会使对象保持活动状态。例如,如果它是此函数中最后完成的事情之一,则它毫无意义(除非特殊情况)。您不再需要它,就像释放一个包含常规字符串对象的变量一样。