lua-users home
lua-l archive

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



> Am 07.04.2019 um 14:07 schrieb Andrew Gierth <andrew@tao11.riddles.org.uk>:
> 
>>>>>> "Marc" == Marc Balmer <marc@msys.ch> writes:
> 
> Marc> In a C  function that is called from Lua, I use the following idiom:
> Marc> char *s;
> Marc> ...
> Marc> s = lua_newuserdata(L, 128);
> 
> Notice that this pushes the Lua value onto the stack, in addition to
> returning a pointer to the data block address.
> 
> While the value is on the Lua stack, it is referenced and therefore will
> not be GC'd. If you drop it from the stack without storing some other
> reference to the value (e.g. by putting it in a table, closure, or
> uservalue), then it will be GC'd when the collector next gets around to
> it. If you did store it somewhere, then it will not be GC'd until after
> all such (strong) references no longer exist or are no longer reachable.
> 
> You should never try and free it explicitly.

Of, so if I understand correctly, if I just call lua_newuserdata and use the memory pointed to it internally in my function, it will get GCed (I am not storing a reference anywhere).