lua-users home
lua-l archive

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

>>>>> "Marc" == Marc Balmer <> writes:

 >> 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.

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

You need to be aware of when exactly you're dropping the value off the
Lua stack, because as soon as you do that, the next API call that
allocates memory could invoke the GC and free your userdata.