Re: Sharing a C object across different lua_States

  • From: Dimiter 'malkia' Stanev <malkia@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 26 Aug 2012 18:49:38 -0700

I would expect not to. As luajit garbage collects only "lua" objects, and cdata, userdata are luajit objects.


But if you have a case where you do:

local someData = ffi.gc( ffi.malloc(10), ffi.C.free )

and convert the someData cdata into pointer, and hand it to another thread, then nothing stops luajit
from collecting it from where it was available

like

someData = nil
collectgarbage()

Only children know how to share :) Yet we as parents as supposed to teach them...
Adult people don't share, they buy/sell or rent!

On 8/26/2012 6:28 PM, Jeff Slutter wrote:
I'm curious if the garbage collector is per-lua_State, or across the
entire process.

For example, if I have two lua_State states (StateA & StateB), and
allocate an ffi struct using ffi.new in StateA. Then, through some
means, get that pointer to StateB for it to use, and then drop my only
reference to it in StateA, will the GC think that data is no longer in
use and free the resource, or will it see that it is still in use in
StateB (assuming I hold a reference in the state)?

One example of this is in William Adam's [great] blog post here:
http://williamaadams.wordpress.com/2012/04/06/passing-parameters-while-creating-threads/

It is obviously working for him (or, so I guess, I know he is on the
mailing list), but I want to make sure it is a safe idea. Well, at least
that the GC won't pull the rug out from under me.



Other related posts: