lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Dec 16, 2011 at 10:06 AM, Luiz Henrique de Figueiredo
<lhf@tecgraf.puc-rio.br> wrote:
>
> If you can use lua_newuserdata, then by all means use it. It depends on
> what library you're binding to Lua. There are two types of libraries
> that use heap objects: those that create (ie allocate) the object
> themselves and those that expect the user to do it. For the first type,
> you use lua_newuserdata for the pointer only and you need to set up a
> gc method to free or close the object. For the second type, you can use
> lua_newuserdata for the complete object and there is no need to box the
> pointer or for a gc method. lbc and lbn are example of the first type;
> lmd5 and lrandom are examples of the second type.
>

And using lua_newuserdata for the whole structure is preferable
because the GC will know the real size of the Lua heap, which is
impossible if an indirection will be used. I saw even a recipe in the
wiki a long time ago where one uses placement new in C++ for doing
just that.

-- 
-alex
http://www.artisancoder.com/