lua-users home
lua-l archive

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


Am 26.07.2016 um 09:24 schröbte Marc Balmer:

Am 25.07.2016 um 21:03 schrieb Roberto Ierusalimschy <roberto@inf.puc-rio.br>:

p = lua_newuserdate(L, sizeof int)
*p = 42

/* Do a Lua API call that might error (longjmp) out */

This has the risk of the memory being invalidated by the
garbage collector.

No, if the userdata is properly anchored (e.g, in the stack). Just
leave the the userdata in the stack (where lua_newuserdate put it)
and everything should work fine.


Thanks to all that replied.  After I sprinkled some lua_checkstack()
calls, the crashes went away.  Time for a release...

Testing with a Lua build that has assertions enabled and/or with a tool like valgrind helps to minimize the chances that you missed a place where a `luaL_checkstack()` would be needed.


Out of curiosity, why does lua_newuserdata() not call lua_checkstack()
internally?  When calling lua_newuserdata(), isn't it quite obvious
that you want the result on the stack?

I'd say performance and consistency. For simple functions like `lua_pushnumber()` it's performance. For complex functions like `lua_newtable()` or `lua_newuserdata()` (which allocate memory anyway) it's consistency with the simple functions. (One exception to this is `lua_pushfstring()`, btw.)


- mb


Philipp