[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: luaL_Buffer and 'exception safety'
- From: Ralph Hempel <rhempel@...>
- Date: Fri, 20 Jul 2007 13:09:28 -0400
Dirk Feytons wrote:
A potential problem I see with this is that if lua_pushlstring()
raises an error I leak the buffer. The chance that this happens is
rather small but since this code will run on an embedded platform I
really can't afford it.
I think the luaL_Buffer() mechanism uses the C stack internally. One
of the problems with embedding Lua is that you're never really sure
how much stack you need, but at least you can look at the source if
you're really interested.
Your code could be written without malloc and use a small local
array like this and achieve the same result as lua_Buffer() like
static int get_data(lua_State* L)
int n = luaL_optint(L, 1, 512 /* some arbitrary default value */);
n = read_data(&buffer, n);
lua_pushlstring(L, &buffer, n);
I'm not a big fan of malloc() in embedded systems, but sometimes
you have no choice :-)
If you knew that get_data() was called from only one instance of a task
then you could just allocate the buffer in BSS.
Everything is a tradeoff...the fact that you can look at the
Lua source and figure out what it's doing is part of why I like
it so much!