如何修复Lua中的“堆栈溢出”错误?

时间:2019-08-09 15:51:14

标签: lua stack-overflow

我正在尝试修复使游戏崩溃的Payday 2 BLT mod,但是我是moad和Lua的初学者,而作者在一年多的时间内都没有更新它,所以我设法找到了崩溃的原因。到脚本的第13行:

这是错误:

Application has crashed: C++ exception
mods/ShadowRaidLoud/lua/coremissionscriptelement.lua:13: stack overflow

据我了解,tostring(self._id)可能是导致错误的原因,但我不知道该怎么办。

这是针对《发薪日2》的mod,删除该mod可能会阻止崩溃,但我想自己修复它。试图与作者联系但没有运气。

coremissionscriptelement.lua:

core:module("CoreMissionScriptElement")
core:import("CoreXml")
core:import("CoreCode")
core:import("CoreClass")

_G.ShadowRaidLoud = _G.ShadowRaidLoud or {}
ShadowRaidLoud = _G.ShadowRaidLoud
ShadowRaidLoud.Run_Script_Data = ShadowRaidLoud.Run_Script_Data or {}

local ShadowRaidLoud_OpenVault = MissionScriptElement.on_executed

function MissionScriptElement:on_executed(instigator, ...)
    local _id = "id_" .. tostring(self._id)   -- stack overflow crash here
    if ShadowRaidLoud and ShadowRaidLoud.Enable and not Network:is_client() then
        if (_id == "id_100961" or _id == "id_100962") and not ShadowRaidLoud.Run_Script_Data[_id] then
            local element = self:get_mission_element(100964)
            if element then             
                local msg = "[System] Vault will open in ".. ShadowRaidLoud.Time4Use.OpenVault .." seconds"
                ShadowRaidLoud:Announce(msg)
                local _tmp = ShadowRaidLoud:Run_Script("id_100964", self, 100964, element, instigator, ShadowRaidLoud.Time4Use.OpenVault)
                ShadowRaidLoud.Run_Script_Data["id_100961"] = _tmp
                ShadowRaidLoud.Run_Script_Data["id_100962"] = _tmp
            end
        end
    end
    ShadowRaidLoud_OpenVault(self, instigator, ...)
end

编辑:我尝试将return添加到函数的最后一行,但它仍然导致此崩溃日志崩溃:

Application has crashed: access violation

-------------------------------

Callstack:

         payday2_win32_release  (???)     ???                                                 


-------------------------------

Current thread: LoadingEnvironment

-------------------------------

System information:
    Application version : 1.92.790
    CPU : Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz (2 cores); SSE; SSE2; SSE3; SSSE3; SSE4.1; SSE4.2
    DirectX : 12.0 
    GPU : NVIDIA GeForce GTX 1070 / nvldumd.dll[26.21.14.3160]
    Language : english
    Memory :     16269MB 264KB
    OS : 6.1.7600 () 0x300-0x1 (64 bits)
    Physics : threaded
    Renderer : DX9 threaded
    Sound : Realtek Semiconductor Corp. (Speakers (Realtek High Definition Audio))

1 个答案:

答案 0 :(得分:1)

很难测试这是否是唯一的问题,但是由于(如已提及@Egor的)hadowRaidLoud_OpenVault == MissionScriptElement.on_executed,因此您应该在ShadowRaidLoud_OpenVault(self, instigator, ...)之前放置return语句,以将其转换为正确的尾调用:

function MissionScriptElement:on_executed(instigator, ...)
    --- SNIP ---
    return ShadowRaidLoud_OpenVault(self, instigator, ...)
end

至少,只要递归一直满足尾部调用的要求,该递归就不会再次导致堆栈溢出。

相关问题