|
发表于 2013-7-5 15:26:01
|
显示全部楼层
回复 4# ddid
我研究了下mushclient wait 函数他的这个写法有点问题
-- ----------------------------------------------------------
-- table of outstanding threads that are waiting
-- ----------------------------------------------------------
local threads = {} -- 一个局部变量
-- ----------------------------------------------------------
-- wait.timer_resume: called by a timer to resume a thread
-- ----------------------------------------------------------
function regexp (regexp, timeout, flags)
local id = "wait_trigger_" .. GetUniqueNumber () --取了一个随机变量
threads [id] = assert (coroutine.running (), "Must be in coroutine")
check (AddTriggerEx (id, regexp,
"-- added by wait.regexp",
bit.bor (flags or 0, -- user-supplied extra flags, like omit from output
trigger_flag.Enabled,
trigger_flag.RegularExpression,
trigger_flag.Temporary,
trigger_flag.Replace,
trigger_flag.OneShot),
custom_colour.NoChange,
0, "", -- wildcard number, sound file name
"wait.trigger_resume",
12, 100)) -- send to script (in case we have to delete the timer)
-- if timeout specified, also add a timer
if timeout and timeout > 0 then
local hours, minutes, seconds = convert_seconds (timeout)
-- if timer fires, it deletes this trigger
check (AddTimer (id, hours, minutes, seconds,
"DeleteTrigger ('" .. id .. "')",
bit.bor (timer_flag.Enabled,
timer_flag.OneShot,
timer_flag.Temporary,
timer_flag.Replace),
"wait.timer_resume"))
-- timer_flag.ActiveWhenClosed, close
check (SetTimerOption (id, "send_to", "12")) -- send to script
-- if trigger fires, it should delete the timer we just added
check (SetTriggerOption (id, "send", "DeleteTimer ('" .. id .. "')"))
end -- if having a timeout
return coroutine.yield () -- return line, wildcards
end -- function regexp
根据lua 的 垃圾回收机制
threads [id]的引用还有 内存代码块就不会给回收,为了解决这个问题,必须改写wait.lua 将local threads 设置成 weak table 并且将 threads={} 然后 collectgarbage() 回收内存 |
|