Lua_getglobal崩溃程序

lua_getglobal crashing program

本文关键字:程序 崩溃 getglobal Lua      更新时间:2023-10-16

我以前写过一篇关于从c中调用lua函数的文章,你可以在这里看看:

Lua:获取全局函数失败后加载文件

如您所见,我正在加载lua文件,然后尝试获取函数并调用它。一位读者建议我解决这个问题的方法是更改为dofile而不是load file,因为您需要执行脚本来访问这些函数。但这不是目前的问题……

无论使用dofile还是loadfile,当我调用lua_getglobal(L, "abc");
我的程序崩溃了

Callstack:

>   Translation.exe!luaS_newlstr(lua_State * L=0xcccccccc, const char * str=0x00460924, unsigned int l=3)  Line 84 + 0x3 bytes  C
    Translation.exe!lua_getfield(lua_State * L=0xcccccccc, int idx=-10002, const char * k=0x00460924)  Line 551 + 0x20 bytes    C
    Translation.exe!LanguageShovel::FileFound(std::basic_string<char,std::char_traits<char>,std::allocator<char> > path="C:LoudResolutionorchidsourceEAWResolutionApplication.cpp")  Line 32 + 0x16 bytes  C++

崩溃:

  for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
       o != NULL;
       o = o->gch.next) {

在Lua库的lstring.c文件中。我不知道在这个lua源代码中到底发生了什么。你觉得会是lua bug吗?还是我做错了?

L=0xcccccccc表明您没有将Lua状态变量正确地传递给此函数(或者它在您的lua_openlua_getglobal调用之间丢失了)。