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?

// c++

class A
int x, y, z;

class B
const A getA() const
return A;

     .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


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?

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.

luabind-user mailing list
[hidden email]