RE: Sharing a C object across different lua_States

  • From: William Adams <william_a_adams@xxxxxxx>
  • To: "luajit@xxxxxxxxxxxxx" <luajit@xxxxxxxxxxxxx>
  • Date: Mon, 27 Aug 2012 05:55:38 +0000

In reference to that blob post I did, there was another one I did about heap 
allocators.  I would say, if you're going to share pointers across threads, 
they better point to stuff that has a fairly long life.
You would not want to pass a pointer that was created with, unless 
you could guarantee and control the lifetime of whatever got allocated.
Same goes for something that is heap allocated.  You'll want to control the 
lifetime.  Is it the receiver that cleans it up, or the allocator?
The GC will definitely kick in if you lose a reference on the allocation side 
of a, so be mindful.
In the case I created, the lua states were in completely separate threads, and 
not related.
-- William
> Date: Sun, 26 Aug 2012 18:49:38 -0700
> From: malkia@xxxxxxxxx
> To: luajit@xxxxxxxxxxxxx
> Subject: Re: Sharing a C object across different lua_States
> 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), )
> 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 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:
> >
> >
> > 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: