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

  • From: William Adams <william_a_adams@xxxxxxx>
  • To: <luajit@xxxxxxxxxxxxx>
  • Date: Thu, 19 Jul 2012 12:27:28 +0000

I'm grateful for this mailing list as I'm learning more

about the early assumptions I made about LuaJIT and

improving my code along the way.

 

I thinkn I have a better appreciation for this 

callback thing, and in particular the scarcity of slots.

 

My most primary use of callbacks is the implicit

callback in the case of the Windows WinProc.

 

The main thing I've picked up, by rereading the docs,

is that I need to explicitly declare __stdcall, and that

the slot allocation this utilizes will be permanent.

 

-- William




----------------------------------------
> Date: Thu, 19 Jul 2012 19:32:53 +0800
> Subject: Re: Making FFI callbacks call free() automatically when it's 
> collected
> From: arch.jslin@xxxxxxxxx
> To: luajit@xxxxxxxxxxxxx
>
> 2012/7/19 Johnson Lin <arch.jslin@xxxxxxxxx>:
> > 2012/7/19 Mike Pall <mike-1207@xxxxxxxxxx>:
> >>
> >> This works. But the time when the GC gets around to collect the
> >> callback slot is not predictable. Since the slots are a scarce
> >> resource, you may run out of them.
> >>
> >> You need know the exact point in time when the callback is no
> >> longer used, anyway. IMHO it's better to create a convenience
> >> wrapper:
> >>
> >> local function wrap_iter(func)
> >> local cb = ffi.cast("CALLBACK", func)
> >> iter(cb)
> >> cb:free()
> >> end
> >>
> >> wrap_iter(function() end)
> >>
> >> --Mike
> >>
> >
> >                                       

Other related posts: