I am using Lua in a microcontroller environment, and recently I had some crashes related to corruption of the heap data.
The issue was tracked down to be the following sequence:
- Call to
lua_pcall(L, 0, 0, 0)without checking the return value.
- Call to another function, again with
lua_pcall(L, 0, 0, 0), this time checking the return value.
- Step 2 fails (as in fact step 1 had failed, but I didn't catch it), and calls
lua_close(L)memory is deallocated. At this stage the memory manager detects a corruption to the heap, and the firmware crashes.
I would like to ask, at which cases can a Lua state be invalid? (And thus no other operations are allowed).
- Is it always safe to call
- If a
lua_pcall()fails, is it allowed to use again
lua_pcall()calling another function?
- In case
lua_pcall()fails with "not enough memory", can any assumptions be made about the memory state? Can
lua_close()be called then, or generally any other Lua API function?