lua-users home
lua-l archive

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


Hi,

Reuben Thomas wrote:
> >     FILE **pf = lua_newuserdata(L, sizeof *pf);
> >     *pf = 0;
> >     luaL_getmetatable(L, LUA_FILEHANDLE);
> >     lua_setmetatable(L, -2);
> >     *pf = /* your FILE* here */
> 
> Why is *pf initially set to NULL?

It's probably not needed in this case, but ...

  lua_newuserdata() does *not* zero the allocated memory!

It's strongly recommended to initialize all fields eventually
used by the __gc metamethod as early as possible.

E.g. an exception might get in the way and then you'll have a bad
userdata object which causes havoc when freed. This is very hard
to debug since this can happen much later.

[ Umm, is this important fact missing in the Lua manual? Well, I
usually check the source instead of the manual anyway ... :-) ]

Bye,
     Mike