Re: ffi.gc prevent collect object

  • From: Mike Pall <mike-1311@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 14 Nov 2013 13:04:03 +0100

Alexey Melnichuk wrote:
> Socket  does not collect while garbage collecting and stay alive until
> lua state is close.

Finalizers for cdata objects are implemented with a weak-keyed
table mapping to the finalizer function. I.e. there's a strong
reference to the function. If it anchors the object through an
upvalue then you get an uncollectable cycle.

I think this is based on a misunderstanding on how cdata
finalizers work: they already get passed the object as the first
argument, there's no need to hold it in an upvalue. And no, you
shouldn't (or can't) 'collect' auxiliary structures such as Lua
tables. Finalizers are for resources, not for memory.

--Mike

Other related posts: