Citizen:/scripting/lua/scheduler.lua:61: 尝试调用一个 nil 值 (upvalue 'fn')

问题描述

所以代码错误是这样的: 此外,lua 代码用于 FiveM 编码,使用 vRP 作为主要框架。 该错误涉及 vRP 上的一个函数,而调用方是工件中的基函数。

即便如此,这也是触发错误的神器代码

Code

How the error looks like

local GetGameTimer = GetGameTimer
local _sbs = Citizen.SubmitBoundaryStart
local coresume,costatus = coroutine.resume,coroutine.status
local debug = debug
local coroutine_close = coroutine.close or (function(c) end) -- 5.3 compatibility
local hadThread = false
local curTime = 0

-- setup msgpack compat
msgpack.set_string('string_compat')
msgpack.set_integer('unsigned')
msgpack.set_array('without_hole')
msgpack.setoption('empty_table_as_array',true)

-- setup json compat
json.version = json._VERSION -- Version compatibility
json.setoption("empty_table_as_array",true)
json.setoption('with_hole',true)

-- temp
local _in = Citizen.InvokeNative

local function FormatStackTrace()
    return _in(`FORMAT_STACK_TRACE` & 0xFFFFFFFF,nil,Citizen.ResultAsString())
end

local function ProfilerEnterScope(scopeName)
    return _in(`PROFILER_ENTER_SCOPE` & 0xFFFFFFFF,scopeName)
end

local function ProfilerExitScope()
    return _in(`PROFILER_EXIT_SCOPE` & 0xFFFFFFFF)
end

local newThreads = {}
local threads = setmetatable({},{
    -- This circumvents undefined behaviour in "next" (and therefore "pairs")
    __newindex = newThreads,-- This is needed for CreateThreadNow to work correctly
    __index = newThreads
})

local boundaryIdx = 1
local runningThread

local function dummyUseBoundary(idx)
    return nil
end

local function getBoundaryFunc(bfn,bid)
    return function(fn,...)
        local boundary = bid or (boundaryIdx + 1)
        boundaryIdx = boundaryIdx + 1
        
        bfn(boundary,coroutine.running())

        local wrap = function(...)
            dummyUseBoundary(boundary)
            
            local v = table.pack(fn(...))
            return table.unpack(v)
        end
        
        local v = table.pack(wrap(...))
        
        bfn(boundary,nil)
        
        return table.unpack(v)
    end
end

解决方法

您的代码的屏幕截图显示了对 getBoundaryFunc 的两次调用

runWithBoundaryStart = getBoundaryFunc(Citizen.SubmitBoundaryStart)
runWithBoundaryEnd = getBoundaryFunc(Citizen.SubmitBoundaryEnd)

为了使 fn 变为 nil,必须在不证明第一个参数的情况下调用任何一个函数。

  1. 了解是否有更多对 getBoundaryFunc 的调用
  2. 找出是否使用 nil 而不是预期的函数值作为第一个参数调用其返回值
  3. 解决这个问题

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...