请教一个问题,lua中类似下面这种代码可能会导致死循环吗?t ={a=100,b=200,c=3,d=4}for i, v in pairs(t) do t[v]=nil print(v)end我们的项目中用到了类似上面的代码,出现了死循环,我们是用另外一个线程来监视主线程来发现死循环的,如果发现一次lua调用的时间超过了1秒就认为进入了死循环,如果认为产生了死循环,会置一个变量,在lua访问一个表的时候判断如果被置了这个变量就会把调用栈打出来
这段代码不会。
请仔细阅读 lua 手册。 迭代 table 过程中,可以置空,但不要增加新的 key 。这一点在手册中强调过。
The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields. In particular, you may clear existing fields. - 回复 | (2318) | 云风 | 2010-08-11 12:11:46