[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How far should one go preventing potential memory leaks?
- From: Sean Conner <sean@...>
- Date: Tue, 10 May 2016 02:56:14 -0400
It was thus said that the Great Marc Balmer once stated:
>
> So above example would essentially become
>
> char *s = lua_newuserdata(L, 32);
> snprintf(s, 32, "bumblebee");
> lua_pushstring(L, s);
>
> 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. 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?
If it's a string and it's really that small, why not?
char s[32];
snprintf(s,sizeof(s),"bumblebee");
lua_pushstring(L,s);
Automatic cleanup and it doesn't change the code all that much.
-spc (Unless I'm missing something ... )