Luabind return value handling with userdata

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Luabind return value handling with userdata

Anssi Penttilä
Hello all,

I'm new at this list though have used luabind in our game project for a while.
Now I have a question which I didn't find an answer from luabind manual or by browsing the archives of this list.

Is there a way to pass class references or pointers from c++ to lua using luabind in such way that it does't need to allocate userdata from lua?
Example:

// c++

class A
{
public:
int x, y, z;
};

class B
{
public:
const A getA() const
{
return A;
}
};

module(L)
[
    class_<A>("A"),
    class_<B>("B")
     .def("getA", &B::getA)    
];

// lua script:

local b = B()
local a = B:getA()

Now when returning from getA luabind seems to allways allocate userdata from lua. 
(in 64 bit environment 64 bytes (sizeof(object_rep)) to be exact)

Well that is not much and garbage collection handles it nicely.

But if I have a loop in lua:

local b = B()

for i=1,1000 do
local a = b:getA() -- each getA allocs memory from lua memory pool

end

and this loop runs 60 times per second then I'm obviously in trouble when lua garbage collector kicks in.

So is there any way in luabind to avoid this? Or use at least less memory?

BR,
Anssi Penttilä


------------------------------------------------------------------------------
Create and publish websites with WebMatrix
Use the most popular FREE web apps or write code yourself;
WebMatrix provides all the features you need to develop and
publish your website. http://p.sf.net/sfu/ms-webmatrix-sf

_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user