Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Lua_pcall的Lua未受保护的错误_Lua - Fatal编程技术网

使用Lua_pcall的Lua未受保护的错误

使用Lua_pcall的Lua未受保护的错误,lua,Lua,我正在使用受Lua保护的呼叫,但我收到一个未受保护的错误: lua_getfield( m_L, LUA_GLOBALSINDEX, "startIteration" ); // 1 if( lua_isfunction( m_L, -1 ) ){ lua_pushnumber( m_L, n ); // 2 auto ret = lua_pcall( m_L, 1, 0, 0 );

我正在使用受Lua保护的呼叫,但我收到一个未受保护的错误:

lua_getfield( m_L, LUA_GLOBALSINDEX, "startIteration" );    // 1
if( lua_isfunction( m_L, -1 ) ){
    lua_pushnumber( m_L, n );                               // 2
    auto ret = lua_pcall( m_L, 1, 0, 0 );                   // 0
    checkLuaReturn( m_L, ret );
}else{
    lua_pop( m_L, 1 );                                      // 0
}
错误是:

PANIC: unprotected error in call to Lua API (../example/ex01.lua:31: attempt to index global 'raster' (a nil value))
Lua代码是:

function startIteration( num )
   io.write( "Start iteration " .. num .. "\n" )
   raster.grass.save( "output.png" )
end
我怎样才能解决它,得到一个真正的保护呼叫


更新:修复了额外的pop

这似乎与您的lua_pcall无关,而是与您的lua代码有关。 行graster.grass.save output.png是错误所在

尝试将其更改为以下内容:

if raster and type(raster) == 'table' then
    if raster.grass and type(raster.grass) then
        raster.grass.save()
    end
end

但是,这并不能解决您的问题,问题是您在创建光栅之前调用变量光栅的成员。

您确定这是实际执行Lua脚本的代码吗?至少打电话给lua_pcall看起来不错,所以问题可能隐藏在其他地方。但是,if分支中的第一个lua_pop存在问题。将0作为第三个参数传递给lua_pcall,这样pop只有在发生错误时才有效。请记住,pcall同时使用堆栈中的函数及其参数。我确信这是调用脚本的地方,我不会在其他地方使用startIteration。此外,lua_pop将移除lua_getfield推送到堆栈中的值。错误值在checkLuaReturn函数中被删除。但关键是:lua\u p从lua\u getfield中调用值。所以在lua_pop之后,你的堆栈是-1而不是0。你是rigth@ComicSansMS,它删除了额外的pop,但错误仍然存在。问题是,既然调用受到保护,为什么lua会恐慌。正如@lhf所说,问题是lua恐慌,而不是lua代码中有错误。无论如何谢谢你