From b94ea923b0b095a5c985cbf8ad45a5ccef3cda94 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Wed, 26 Jun 2019 15:18:07 +0200 Subject: [PATCH] scheduled function: replacing new by malloc needs to initialize complex members (#6233) * scheduled function: replacing new by malloc needs to initialize complex members Functional was not initialized because of malloc() instead of new. First assignment calls destructor on initial value which was not constructed (->frozen,wdt). --- cores/esp8266/Schedule.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/Schedule.cpp b/cores/esp8266/Schedule.cpp index 33c3b16e3..f8d7b6791 100644 --- a/cores/esp8266/Schedule.cpp +++ b/cores/esp8266/Schedule.cpp @@ -41,14 +41,18 @@ static scheduled_fn_t* get_fn_unsafe () { result = sUnused; sUnused = sUnused->mNext; - result->mNext = nullptr; } // if no unused items, and count not too high, allocate a new one else if (sCount < SCHEDULED_FN_MAX_COUNT) { + // calling malloc instead of new to avoid exception raising while in ISR + // construct complex members in place (never raises exceptions) result = (scheduled_fn_t*)malloc(sizeof(scheduled_fn_t)); if (result) + { + new (&result->mFunc) decltype(result->mFunc)(); ++sCount; + } } return result; } @@ -70,6 +74,7 @@ bool schedule_function (const std::function& fn) return false; item->mFunc = fn; + item->mNext = nullptr; if (sFirst) sLast->mNext = item;