lua-users home
lua-l archive

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


> Am 26.07.2016 um 09:42 schrieb Thomas Jericke <tjericke@indel.ch>:
> 
> On 07/26/2016 09:24 AM, Marc Balmer wrote:
>>> 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...
>> 
>> 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?
>> 
>> - mb
>> 
>> 
> To my knowledge, performance.
> You should know how many stack slots you have available in your C code. Just call lua_checkstack with the number of slots you need at the beginning.
> And if you are called by Lua you always have some slots reserved already (I think 20 by default).

Ok, I see.  That makes sense.

I am adding this ti all my modules now, this is a happy day ;)