lua-users home
lua-l archive

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


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