Re: Making FFI callbacks call free() automatically when it's collected

  • From: Johnson Lin <arch.jslin@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 19 Jul 2012 20:40:04 +0800

2012/7/19 Mike Pall <mike-1207@xxxxxxxxxx>:
> Johnson Lin wrote:
>> That's really strange though, since the finalizer of the object hasn't
>> actually returned yet, there should still be a ref in the Lua VM to
>> the object, no? How come the weak-key table can lost track of that
>> beforehand?
>
> Only userdata objects are guaranteed to be kept in weak-key tables
> until the finalizer is called. There's no such guarantee for cdata
> objects.
>
> [It may turn out to be a quite expensive decision to change that.
> I don't want to decide this before I'm sure it'll play nicely with
> the new GC.]
>
> --Mike
>

Ouch, then I have to find another way around this. It seems that it's
getting more and more unlikely that I can get away with an automatic
callback management on UI callbacks.

But it's like you have to remember to call obj:remove_your_callbacks()
every time when there's some UI object goes out of scope or going to
be deleted or it's ref becomes 0 on other unnoticed occasions. It
would be pretty error-prone... But, considering how much more work
would passing callbacks from Lua to C be if not using FFI callbacks at
all....

That said, now I know it may be expensive to change the behaviour of
the cdata finalizer, but IMHO it's pretty important just for
consistency's sake, that it has the same behaviour as userdata / 5.2's
table __gc, so people can be spared from keeping notes of these
subtleties .... This is not limited to FFI callbacks' topic though, I
can find other ways around this particular case, but there must still
be usages of cdata as weak-keys in other scenario. Just my 2 cents. :)

best,
Johnson

Other related posts: