Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help understanding how luabind instantiates classes

Let's say I have a class like this:

class A
{
public:
A(){}
~A(){}
};

And expose it to Lua via Luabind like this:

module(luaState) 
    [
        class_<A>("Foo")
        .def(constructor<>())
    ];

And finally instantiate it in a script like this:

A = Foo();

What is the actual 'state of existence' of A at that point?

Is it somewhere in the heap, and lua keeps a reference to it somewhere? (or a luabind::object?)

I have the feeling that it can only be a pointer, as in, allocated by new or equivalent.

However, I can bind functions to lua that accept references, like lua_doSomething(A & a) and what ends up in there will be an actual reference. Granted I know this could very well just be luabind passing a as *a, but I have no idea if that's how it happens.

The reason I am asking this is to understand and predict the lifetime of objects instantiated in a script at little better.

That, and me being unsure if ownerships or lifetimes change if, instead of exposing the class to lua like above, I do it like this:

A * lua_CreateA()
{
return new A();
}

module(luaState) 
    [
        class_<A>("Foo")
    ];


module(luaState)
    [

    def("createA",&lua_CreateA)

    ];

And using it like

A = createA();

By the logic I understand so far, this case would require me to do the cleanup since I'm the one allocating a new object, unless assignment like this for luabind would be the same as doing it with the bound constructor.

In short, I'm really really confused about object lifetime and stuff here... I googled for the keywords related to this, but I'm only getting stuff like http://www.gamedev.net/topic/525692-luabind-ownership-and-destruction/

Which isn't really what I want to know. I want to understand the concrete way how things are handled behind the scenes regarding allocation, instantiation, lifetime and all that.

like image 668
Erius Avatar asked Aug 14 '11 20:08

Erius


1 Answers

Object lifetime is very simple with Luabind. You don't need to understand the gory details of what Luabind does internally to get it.

Lua owns the objects that are created by Lua directly. So if you allocate an object with constructor syntax in Lua, that object is owned by Lua. When the object is no longer referenced in Lua, the Lua GC will collect it.

If Lua gets a reference to an object by any other means, then Lua does not own that object unless you specifically transfer ownership using Luabind's adopt policy. Therefore, if you are binding a function that returns an object to Lua, such that you now expect Lua to decide when that object is alive or not, then you need to use the adopt policy.

The last paragraph only applies to actual references (returning pointers and reference types). If Lua is given a copy of an object (via a non-reference or pointer return value), then Lua will own that copy of the object.

like image 148
Nicol Bolas Avatar answered Oct 23 '22 19:10

Nicol Bolas