lua_getglobal导致程序崩溃。

我之前发布了一篇帖子,关于试图从 C 调用 lua 函数。你可以在这里查看:

Lua:在加载文件后无法获取全局函数

正如你所看到的,我正在加载 lua 文件,然后尝试获取并调用函数。有一位读者建议我解决这个问题的方法是改为使用 dofile 而不是 loadfile,因为需要执行脚本才能访问这些函数。但这不是手头的问题……

无论使用 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:\Loud\Resolution\orchid\source\EAWResolutionApplication.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 错误吗?还是我只是在错误地做这件事?

运行环境:Windows xp 32 位。

原文链接 https://stackoverflow.com/questions/6754778

点赞
stackoverflow用户596285
stackoverflow用户596285
`L=0xcccccccc` 表示你没有正确地将 Lua 状态变量传递给此函数(或者在你的 `lua_open` 和 `lua_getglobal` 调用之间丢失了它)。
2011-07-19 23:42:29