From 69a18756fc775df88e46f1c0aecca5b1effc08fa Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Wed, 26 Jun 2019 13:53:38 +0200 Subject: [PATCH 1/3] 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 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/Schedule.cpp b/cores/esp8266/Schedule.cpp index 33c3b16e38..9298613170 100644 --- a/cores/esp8266/Schedule.cpp +++ b/cores/esp8266/Schedule.cpp @@ -41,12 +41,12 @@ 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) { result = (scheduled_fn_t*)malloc(sizeof(scheduled_fn_t)); + new (&result->mFunc) decltype(result->mFunc)(); if (result) ++sCount; } @@ -70,6 +70,7 @@ bool schedule_function (const std::function& fn) return false; item->mFunc = fn; + item->mNext = nullptr; if (sFirst) sLast->mNext = item; From 8797a7a6df1ba1572adc4c55d6fed696bed873a5 Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Wed, 26 Jun 2019 14:02:07 +0200 Subject: [PATCH 2/3] add comment --- cores/esp8266/Schedule.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cores/esp8266/Schedule.cpp b/cores/esp8266/Schedule.cpp index 9298613170..3d752a092b 100644 --- a/cores/esp8266/Schedule.cpp +++ b/cores/esp8266/Schedule.cpp @@ -45,6 +45,8 @@ static scheduled_fn_t* get_fn_unsafe () // 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)); new (&result->mFunc) decltype(result->mFunc)(); if (result) From 21b582d86e66777ca32df1188bc6c55bf68b1bf8 Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Wed, 26 Jun 2019 14:28:50 +0200 Subject: [PATCH 3/3] ... only when malloc succeeds --- cores/esp8266/Schedule.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/Schedule.cpp b/cores/esp8266/Schedule.cpp index 3d752a092b..f8d7b67916 100644 --- a/cores/esp8266/Schedule.cpp +++ b/cores/esp8266/Schedule.cpp @@ -48,9 +48,11 @@ static scheduled_fn_t* get_fn_unsafe () // 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)); - new (&result->mFunc) decltype(result->mFunc)(); if (result) + { + new (&result->mFunc) decltype(result->mFunc)(); ++sCount; + } } return result; }