From 7bead89e39226d8e43bb3951377406a588e7cd09 Mon Sep 17 00:00:00 2001 From: red031000 Date: Tue, 7 Dec 2021 20:43:22 +0000 Subject: [PATCH] fix a bug with timer erasing --- dGame/Entity.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index c98323a..1d31eb8 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -1222,13 +1222,19 @@ void Entity::UpdateXMLDoc(tinyxml2::XMLDocument* doc) { } void Entity::Update(const float deltaTime) { - for (int i = 0; i < m_Timers.size(); i++) { + int timerSize = m_Timers.size(); + for (int i = 0; i < timerSize; i++) { m_Timers[i]->Update(deltaTime); if (m_Timers[i]->GetTime() <= 0) { const auto timerName = m_Timers[i]->GetName(); - delete m_Timers[i]; - m_Timers.erase(m_Timers.begin() + i); + do { //sometimes, due to a race condition, m_Timers.erase doesn't actually erase, repeat until it does + if (m_Timers[i]->GetName() != timerName) { + break; + } + delete m_Timers[i]; + m_Timers.erase(m_Timers.begin() + i); + } while (m_Timers.size() == timerSize); //timer size indicates whether it's actually successfully been erased or not for (CppScripts::Script* script : CppScripts::GetEntityScripts(this)) { script->OnTimerDone(this, timerName);