[Lua]C++调用完lua_pcall()函数后,只因没调用lua_pop()导致栈溢出!

2015-03-16
本帖最后由 书径尘 于 2015-3-16 09:46 编辑

先贴下代码:
  1.         std::string CallLuaFunction(lua_State* pLuaState, const char* szFunction)
  2.         {
  3.                 std::string strFunctionName;
  4.                 const char* szParam = ParseFunctionName(szFunction, strFunctionName);

  5.                 // get lua function
  6.                 lua_getglobal(pLuaState, strFunctionName.c_str());        // 将lua脚本文件中的目标函数压入到堆栈

  7.                 int paramCount = 0;
  8.                 int returnCount = 1;

  9.                 // 将各参数依次入栈
  10.                 if (szParam[0] != 0)
  11.                 {
  12.                         const char* szCurParam = szParam;

  13.                         while (szCurParam[0] != 0)
  14.                         {
  15.                                 std::string strCurParamValue;
  16.                                 bool bNumber = false;
  17.                                 szCurParam = ParseNextParam(szCurParam, strCurParamValue, &bNumber);

  18.                                 if (bNumber)
  19.                                 {
  20.                                         double value = atof(strCurParamValue.c_str());
  21.                                         lua_pushnumber(pLuaState, value);
  22.                                 }
  23.                                 else
  24.                                 {
  25.                                         lua_pushstring(pLuaState, strCurParamValue.c_str());
  26.                                 }

  27.                                 paramCount++;
  28.                         }
  29.                 }

  30.                 // 调用脚本函数
  31.                 if (lua_pcall(pLuaState, paramCount, returnCount, 0) != 0)        // 执行刚才被压入堆栈中的函数,最后两个参数分别是参数的数量和返回值的数量, 函数执行后会删除原来被压入到堆栈中的数据,接着会把返回值压入到堆栈
  32.                 {
  33.                         ErrorBox("error running function `%s': %s", szFunction, lua_tostring(pLuaState, -1));
  34.                         return "";
  35.                 }

  36.                 std::string strReturn;

  37.                 // 获取返回值
  38.                 {
  39.                         int iReturn = -returnCount;

  40.                         if (lua_isnumber(pLuaState, iReturn))
  41.                         {
  42.                                 double value = lua_tonumber(pLuaState, iReturn);

  43.                                 char szStr[32];
  44.                                 sprintf_s(szStr, sizeof(szStr), "%f", value);
  45.                                 strReturn = szStr;
  46.                         }
  47.                         else if (lua_isstring(pLuaState, iReturn))
  48.                         {
  49.                                 const char* szValue = lua_tostring(pLuaState, iReturn);
  50.                                 strReturn = szValue;
  51.                         }
  52.                 }

  53.                 lua_pop(pLuaState, 1);

  54.                 return strReturn;
  55.         }
复制代码


被调用的函数(空函数):
function Script_Main()
end

看到上面最后一行lua_pop()函数了吧,如果不加这条代码,就会导致堆栈溢出(必须用while()死循环不停的调用这个lua函数才会发生,否则,重现不了,这个问题害的我花了整整一天去重现这个bug)

发这贴子,我就是想问,是不是调用了lua_getglobal()后,必须调用 lua_pop()函数?否则会堆栈溢出?
否则是什么原因呢?


最新评论
暂无评论
参与评论

类魂游戏设计专题
推广
商务合作 查看更多

编辑推荐 查看更多
【爆款新游】【潜力佳作】分析系列
推广