Skip to content

Commit b94ea92

Browse files
authored
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).
1 parent 909a9c4 commit b94ea92

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

cores/esp8266/Schedule.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ static scheduled_fn_t* get_fn_unsafe ()
4141
{
4242
result = sUnused;
4343
sUnused = sUnused->mNext;
44-
result->mNext = nullptr;
4544
}
4645
// if no unused items, and count not too high, allocate a new one
4746
else if (sCount < SCHEDULED_FN_MAX_COUNT)
4847
{
48+
// calling malloc instead of new to avoid exception raising while in ISR
49+
// construct complex members in place (never raises exceptions)
4950
result = (scheduled_fn_t*)malloc(sizeof(scheduled_fn_t));
5051
if (result)
52+
{
53+
new (&result->mFunc) decltype(result->mFunc)();
5154
++sCount;
55+
}
5256
}
5357
return result;
5458
}
@@ -70,6 +74,7 @@ bool schedule_function (const std::function<void(void)>& fn)
7074
return false;
7175

7276
item->mFunc = fn;
77+
item->mNext = nullptr;
7378

7479
if (sFirst)
7580
sLast->mNext = item;

0 commit comments

Comments
 (0)