Hi,
So I tried a simple test to work off a char array and back it up:
#include <string>
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
char buffer[4096];
char backupBuffer[4096];
char *pTop = buffer;
void* customLuaAlloc(void* userData, void* ptr, size_t oldSize, size_t newSize)
{
if (newSize == 0)
{
//free(ptr);
return 0;
}
else {
//return realloc(ptr, newSize);
if (ptr != NULL) {
// Copy contents
memcpy(pTop, ptr, oldSize);
}
ptr = NULL;
char *pOldTop = pTop;
pTop += newSize;
return (void*)pOldTop;
}
}
int main()
{
lua_State* L = luaL_newstate();
lua_setallocf(L, customLuaAlloc, NULL);
luaL_openlibs(L);
luaL_dostring(L, "a = 1");
memcpy(backupBuffer, buffer, 4096); // this causes a crash
luaL_dostring(L, "a = 2");
//memcpy(buffer, backupBuffer, 4096); // try restoring 'a' here once back up works
luaL_dostring(L, "print(a)");
lua_close(L);
}
If I comment out: memcpy(backupBuffer, buffer, 4096) then things are fine. Otherwise a crash at:
> LuaTTD.exe!luaH_getshortstr(Table * t, TString * key) Line 544 C
LuaTTD.exe!luaT_gettmbyobj(lua_State * L, const lua_TValue * o, TMS event) Line 82 C
LuaTTD.exe!GCTM(lua_State * L, int propagateerrors) Line 812 C
LuaTTD.exe!callallpendingfinalizers(lua_State * L) Line 862 C
LuaTTD.exe!luaC_freeallobjects(lua_State * L) Line 971 C
LuaTTD.exe!close_state(lua_State * L) Line 245 C
LuaTTD.exe!lua_close(lua_State * L) Line 344 C
LuaTTD.exe!main() Line 50 C++
LuaTTD.exe!invoke_main() Line 78 C++
LuaTTD.exe!__scrt_common_main_seh() Line 288 C++
LuaTTD.exe!__scrt_common_main() Line 331 C++
LuaTTD.exe!mainCRTStartup() Line 17 C++
Trying to figure out why.
Thanks,
Abhijit