lua-users home
lua-l archive

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


On 05/10/2016 08:41 AM, Marc Balmer wrote:
This prevents a memory leak if lua_pushstring() raises an error, but I will end up with a number of rather small allocations that are only used for a short
period of time.
If they buffers are really that small, I would just use the stack.

char s[32];
snprintf(s, sizeof(s), "bumblebee");
lua_pushstring(L, s);

After all, this doesn't use more stack space then having 4 local doubles in your C function.

Wouldn't it be nice to have a explicit call in the Lua API to
free a userdata value immediately, e.g.

char *s = lua_newuserdata(L, 32);
snprintf(s, 32, "bumblebee");
lua_pushstring(L, s);

lua_freeuserdata(s)

Or do you think the garbage colletor overhead can be neglected?

I think such a function would only reduce memory overhead and not CPU overhead. lua_freeuserdata(s) would have to check whether there is still a valid reverence to that userdata.
--
Thomas