diff --git a/Source/Engine/Threading/Task.cpp b/Source/Engine/Threading/Task.cpp index bab794728..a640019d1 100644 --- a/Source/Engine/Threading/Task.cpp +++ b/Source/Engine/Threading/Task.cpp @@ -226,6 +226,27 @@ void Task::OnEnd() { ASSERT(!IsRunning()); - // Add to delete - DeleteObject(30.0f, false); + if (_continueWith && !_continueWith->IsEnded()) + { + // Let next task do the cleanup (to ensure whole tasks chain shares the lifetime) + _continueWith->_rootForRemoval = _rootForRemoval ? _rootForRemoval : this; + } + else + { + constexpr float timeToLive = 30.0f; + + // Remove task chain starting from the root + if (_rootForRemoval) + { + auto task = _rootForRemoval; + while (task != this) + { + task->DeleteObject(timeToLive, false); + task = task->_continueWith; + } + } + + // Add to delete + DeleteObject(timeToLive, false); + } } diff --git a/Source/Engine/Threading/Task.h b/Source/Engine/Threading/Task.h index eb450effd..19fbf4fad 100644 --- a/Source/Engine/Threading/Task.h +++ b/Source/Engine/Threading/Task.h @@ -63,6 +63,11 @@ protected: /// Task* _continueWith = nullptr; + /// + /// The task that's starts removal chain, used to sync whole task chain lifetime. + /// + Task* _rootForRemoval = nullptr; + void SetState(TaskState state) { Platform::AtomicStore((int64 volatile*)&_state, (uint64)state);