Re: Questions about luabind::object

Jason McKesson <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
That's great. I've got a few more questions.

Each luabind::object has a lua_State, which is the state it was created 
with. If you create a lua_State with lua_newthread, or you are in a 
function that is called from a coroutine, then the lua_State will be 
different from the main lua_State. Indeed, these lua_State objects are 
subject to garbage collection, which could render your objects unusable.

So, if you have a luabind::object from a thread state, and you want to 
move the value to the main state, how would you go about doing that? The 
obvious safe way is to put it in a global variable, then create an 
object in the main state from that same global variable (and remembering 
to nil the global variable away). But is there some other, cleaner way 
to move objects from state to state?


Iliya Trendafilov wrote:
> luabind::object keeps objects in the Lua registry:
> http://www.lua.org/manual/5.1/manual.html#3.5
> So you can safely pop your object from the stack and it won't be
> garbage collected. The lifetime of the object is indeed controlled by
> TheChunk - luabind::object will remove the value it holds in its
> destructor (before you ask, copying luabind::objects is ok and storing
> different luabind::object instances holding the same Lua object works
> as expected - the Lua object is removed from the registry and
> therefore left vulnerable for garbage collecting only when the last
> instance of luabind::object is destroyed)
>
> As for your first question, I believe construction of luabind::object
> doesn't have any reasons to modify the stack. And it's easy to verify
> that in practice.
>
> On Thu, Dec 3, 2009 at 9:14 AM, Jason McKesson <[email protected]> wrote:
>   
>> I have a question about the interaction with the stack for
>> luabind::object instances and lifetimes.
>>
>> OK, so I have a lua chunk. I have loaded it with lua_load. Therefore, it
>> is on the stack at index -1. So if I do:
>>
>> luabind::object TheChunk(luabind::from_stack(pState, -1))
>>
>> The variable "TheChunk" will hold this function.
>>
>> So question 1: Is the function still on the stack? It looks like it is
>> from the documentation, as it says that "This will create the object o
>> and copy the value from the top of the lua stack."
>>
>> Question 2: If it is still on the stack, what happens to the object if I
>> pop it off of the stack? Is the object in danger of being garbage
>> collected after popping it, or is the object lifetime now controlled by
>> the lifetime of "TheChunk"?
>>
>> Question 3: If the object holds a copy, exactly where is this copy? If
>> the copy is considered a valid Lua reference (thus making it ineligible
>> for garbage collection), how exactly does that work? How does Luabind
>> accomplish this?
>>
>> ------------------------------------------------------------------------------
>> Join us December 9, 2009 for the Red Hat Virtual Experience,
>> a free event focused on virtualization and cloud computing.
>> Attend in-depth sessions from your desk. Your couch. Anywhere.
>> http://p.sf.net/sfu/redhat-sfdev2dev
>> _______________________________________________
>> luabind-user mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/luabind-user
>>
>>     
>
> ------------------------------------------------------------------------------
> Join us December 9, 2009 for the Red Hat Virtual Experience,
> a free event focused on virtualization and cloud computing. 
> Attend in-depth sessions from your desk. Your couch. Anywhere.
> http://p.sf.net/sfu/redhat-sfdev2dev
> _______________________________________________
> luabind-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
>   


------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing. 
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.