From 5941fa5dd9f56fe349f71ccd00ba6fa029327d68 Mon Sep 17 00:00:00 2001 From: Per Inge Mathisen Date: Wed, 29 Jun 2011 23:10:30 +0200 Subject: [PATCH] Fix race conditions in timer execution for qtscript code. Patch reviewed by Safety0ff. Problem found by Vexed. --- src/qtscript.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/qtscript.cpp b/src/qtscript.cpp index ac0ecae57..425ae832f 100644 --- a/src/qtscript.cpp +++ b/src/qtscript.cpp @@ -197,20 +197,20 @@ bool updateScripts() } // Check for timers, and run them if applicable. // TODO - load balancing - QMutableListIterator iter(timers); + QList runlist; // make a new list here, since we might trample all over the timer list during execution + for (int i = 0; i < timers.size(); i++) + { + if (timers.at(i).frameTime <= gameTime) + { + timers[i].frameTime = timers.at(i).ms + gameTime; // update for next invokation + runlist.append(timers.at(i)); + } + } + QListIterator iter(runlist); while (iter.hasNext()) { - timerNode node = iter.next(); - if (node.frameTime <= gameTime) - { - node.frameTime = node.ms + gameTime; // update for next invokation - callFunction(node.engine, node.function, QScriptValueList()); - } - // Node could have been brutally removed from underneath us at this point - if (timers.contains(node)) - { - iter.setValue(node); - } + const timerNode node = iter.next(); + callFunction(node.engine, node.function, QScriptValueList()); } return true; }