From d8bb51902f77ef48ec6757a97808d9ea05b7e5ae Mon Sep 17 00:00:00 2001 From: Gregg Date: Wed, 24 Nov 2021 18:27:55 -0600 Subject: [PATCH] Fixed Bug in TimedWrites The loop over TimedWrites incorrectly erased iterators inside a for-loop. For some reason this never caused an issue on the ESP32, but crashed on the ESP32-C3. Solution is to change the for-loop to a while-loop with proper handling of the iterator when an element is deleted. This appears to fix the problem. --- src/HAP.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index a079b72..2120511 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -1285,14 +1285,18 @@ void HAPClient::checkTimedWrites(){ unsigned long cTime=millis(); // get current time char c[64]; - - for(auto tw=homeSpan.TimedWrites.begin(); tw!=homeSpan.TimedWrites.end(); tw++){ // loop over all Timed Writes using an iterator + + auto tw=homeSpan.TimedWrites.begin(); + while(tw!=homeSpan.TimedWrites.end()){ if(cTime>tw->second){ // timer has expired sprintf(c,"Removing PID=%llu ALARM=%u\n",tw->first,tw->second); LOG2(c); - homeSpan.TimedWrites.erase(tw); + tw=homeSpan.TimedWrites.erase(tw); } + else + tw++; } + } //////////////////////////////////////