Read in Python CFFI documentation:
The interface is based on LuaJIT’s FFI (...)
Read on LuaJIT website (about 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)
So, using Python-CFFI, do you have to trigger the destruction of the last reference to a variable instantiated using ffi.gc
(= that needs a special function for deallocation because some parts of it are dynamically allocated) by setting it to (i.e.) ffi.NULL
?
Python is designed so that all objects are garbage collected as soon as there is no more reference to it (or soon afterwards), like any other garbage-collected language (including Lua). The trick of setting p = None
explicitly (or del p
) will merely make sure that this local variable p
does not keep the object alive. It is pointless (barring special cases) if, for example, it is one of the last thing done in this function. You don't need it any more than you need it to free, say, a variable that would contain a regular string object.