SIGSEGV错误在一些lua / c ++代码中 [英] SIGSEGV error in some lua/c++ code

查看:139
本文介绍了SIGSEGV错误在一些lua / c ++代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码以SIGSEGV错误结束:

The following code end in a SIGSEGV error:

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)


推荐答案

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

You have to do create a new lua state before opening the lib (luaL_openlibs(L);), like this:

L = luaL_newstate();

您会得到分段错误,因为您有一个单位化的指针,解引用它)是未定义的行为。

You get the segmentation fault because you have an unitialized pointer, dereferencing it (which is what the lib does) is undefined behavior.

这篇关于SIGSEGV错误在一些lua / c ++代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆