> If I comment out: memcpy(backupBuffer, buffer, 4096) then things are fine.
No they are not. Your buffers are too small and you have a buffer overrun in any case. However, with the memcpy in place, it modifies the the content of the overrun memory so you see the problem earlier.
Another problem is that you call luaL_newstate() and then lua_setallocf(). The first call will already have allocated some memory using the default allocator. This is benign for now, but may bite you in future. Use lua_newstate() instead.
Cheers,
V.