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.