diff --git a/stdlib/public/Concurrency/TaskLocal.cpp b/stdlib/public/Concurrency/TaskLocal.cpp index b76a4fec1e0d5..fb7e380b4ce61 100644 --- a/stdlib/public/Concurrency/TaskLocal.cpp +++ b/stdlib/public/Concurrency/TaskLocal.cpp @@ -250,7 +250,12 @@ void TaskLocal::Storage::initializeLinkParent(AsyncTask* task, TaskLocal::MarkerItem *TaskLocal::MarkerItem::create(AsyncTask *task, Item *next, Kind kind) { size_t amountToAllocate = sizeof(MarkerItem); - void *allocation = _swift_task_alloc_specific(task, amountToAllocate); + void *allocation; + + // If we have a task, allocate from that task. If not, use malloc. This must + // mirror the corresponding dealloc/free call in Item::destroy. + if (task) allocation = _swift_task_alloc_specific(task, amountToAllocate); + else allocation = malloc(amountToAllocate); return new (allocation) MarkerItem(next, kind); }