Re: How far should one go preventing potential memory leaks?
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: How far should one go preventing potential memory leaks?
- From: Thomas Jericke <tjericke@...>
- Date: 2016年5月10日 09:10:39 +0200
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