某些lua / c ++代码中的SIGSEGV错误

时间:2013-02-12 19:49:41

标签: c++ lua sigsegv

以下代码以SIGSEGV错误结束:

extern "C" {
    #include "lua/lua.h"
    #include "lua/lualib.h"
    #include "lua/lauxlib.h"
}

int main( int argc, char *argv[] )
{
    lua_State *L;
    luaL_openlibs(L);
    lua_close(L);
    return 0;
}

Gdb给了我以下内容:

(gdb) run
Starting program: d:\Dropbox\cpp\engine\bin\main.exe
[New Thread 7008.0x1df8]

Program received signal SIGSEGV, Segmentation fault.
0x6d781f30 in lua_pushcclosure () from d:\Dropbox\cpp\engine\bin\lua52.dll
(gdb) where
#0  0x6d781f30 in lua_pushcclosure () from d:\Dropbox\cpp\engine\bin\lua52.dll
#1  0x6d79329e in luaL_requiref () from d:\Dropbox\cpp\engine\bin\lua52.dll
#2  0x6d79bdee in luaL_openlibs () from d:\Dropbox\cpp\engine\bin\lua52.dll
#3  0x004013a6 in main (argc=1, argv=0x702fc8) at main.cpp:10
(gdb)

1 个答案:

答案 0 :(得分:6)

你必须在打开lib(luaL_openlibs(L);)之前创建一个新的lua状态,如下所示:

L = luaL_newstate();

你得到了分段错误,因为你有一个整体化的指针,解除引用它(这是lib所做的)是未定义的行为。