[英]C++ & Lua - attempt to index a nil value error
I'm currently learning how to make Lua work with C++ and i stumbled upon this problem. 我目前正在学习如何使Lua与C ++一起使用,但我偶然发现了这个问题。 Here is my little app that I'm currently using: 这是我目前正在使用的小应用程序:
#include <lua.5.2.3\src\lua.hpp>
#include <iostream>
#include <string>
int pluacall(lua_State *L){
std::cout << "Called from inside Lua." << std::endl;
return 0;
}
int main(){
std::string x;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_Reg _funcs[] = { { "CppFunc", pluacall }, {} };
if (luaL_dofile(L, "test.lua")){
std::cout << "Error: " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
luaL_setfuncs(L, _funcs, 0);
lua_getglobal(L, "sup");
if (lua_pcall(L, 0, 0, 0)){
std::cout << "Error: " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
std::cout << "Test";
std::getline(std::cin, x);
lua_close(L);
return 0;
}
Everything worked flawlessly until I added the luaL_setfuncs(...)
. 在我添加luaL_setfuncs(...)
之前,一切都可以正常工作。
Now the app crashes with the following error: 现在,应用程序崩溃并出现以下错误:
PANIC: Unprotected error in call to Lua API (attempt to index a nil value)
I'm gonna be completely honest I have absolutely no idea why it doesn't work or what this error even means (google wasn't my friend today). 我要说实话,我绝对不知道为什么它不起作用或者这个错误甚至意味着什么(谷歌今天不是我的朋友)。 Any ideas? 有任何想法吗?
It's probably also worth mentioning that I do not link the Lua library with my app, I compile them together (all Lua source added to project) 还可能值得一提的是,我没有将Lua库与我的应用程序链接,而是将它们编译在一起(所有Lua源代码都添加到了项目中)
Edit: here's the Lua script I'm testing it with 编辑:这是我正在测试的Lua脚本
function sup()
io.write("Hey.\n")
CppFunc()
return 1
end
void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
Registers all functions in the array l (see luaL_Reg) into the table on the top of the stack (below optional upvalues, see next). 将数组l中的所有函数(请参见luaL_Reg)注册到堆栈顶部的表中 (在可选的upvalue下方,请参见下一个)。
void luaL_newlib (lua_State *L, const luaL_Reg *l);
Creates a new table and registers there the functions in list l. 创建一个新表并在其中注册列表l中的功能。 It is implemented as the following macro: 它通过以下宏实现:
(luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
Either insert luaL_newlibtable(L, _funcs)
before luaL_setfuncs
, or use luaL_newlib(L, _funcs)
instead. 任一插入luaL_newlibtable(L, _funcs)
之前luaL_setfuncs
,或使用luaL_newlib(L, _funcs)
代替。 If you want to register functions not into separate table, but into global namespace, you may use following approach: 如果您不想将功能注册到单独的表中,而是注册到全局名称空间中,则可以使用以下方法:
lua_pushvalue(L, LUA_GLOBALSINDEX); // push _G table
luaL_setfuncs(L, _funcs, 0); // set funcs on _G
lua_pop(L, 1); // pop _G
Try explicitly setting the sentinel that terminates funcs array: 尝试显式设置终止funcs数组的哨兵:
luaL_Reg _funcs[] = { { "CppFunc", pluacall }, {NULL, NULL} };
I don't think that the compiler gives them default 0 values (but I'm not 100%), so if you don't set them you could end up with garbage pointers hence crash. 我不认为编译器会为它们提供默认的0值(但我不是100%),因此,如果不设置它们,最终可能会导致垃圾指针,从而导致崩溃。
EDIT after comment that the above does not help: 注释后编辑,上面的内容无济于事:
If this is not the issue, try using luaL_newlib instead of luaL_setfuncs: it appears to use luaL_newlibtable which creates a table on the stack then calls setfuncs to register functions in it: 如果这不是问题,请尝试使用luaL_newlib代替luaL_setfuncs:它似乎使用luaL_newlibtable在表上创建一个表,然后调用setfuncs在其中注册函数:
luaL_newlib(L, _funcs);
(I'm surprised at how difficult it is to find clear docs/examples about this). (令我惊讶的是,找到与此相关的清晰文档/示例非常困难)。 See this very good answer to similar question on SO . 在SO上看到对类似问题的很好回答 。
So I did not exactly solve this problem how I thought I would, however it's better than nothing. 因此,我并没有完全按照自己的想法解决此问题,但是总比没有好。
I changed luaL_setfuncs()
to lua_register()
and everything seems to work now. 我将luaL_setfuncs()
更改为lua_register()
,现在一切似乎都可以工作了。 What was the problem - i have no idea. 有什么问题-我不知道。
I thought luaL_setfuncs()
was the preffered way of registering C functions with Lua now, well it doesn't work so I'll have to stick with registering everything through lua_register()
. 我以为luaL_setfuncs()
是现在向Lua注册C函数的luaL_setfuncs()
方法,但是它不起作用,所以我不得不坚持通过lua_register()
注册所有内容。
If I get any more info on this issue I'll edit this post. 如果我获得有关此问题的更多信息,我将编辑此帖子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.