From 025764d2d89bd31722049d160f6ffe16ce1c103c Mon Sep 17 00:00:00 2001 From: Pavel Solodovnikov Date: Sat, 6 Jan 2024 17:19:55 +0300 Subject: [PATCH] droid.cpp: Fix `DROID` dtor segfault for transports When a transport has some droids in it while its dtor is being called, the code that frees all droids in its transport group will segfault because each `delete psCurr` will modify the list and hence invalidate the current iterator. Signed-off-by: Pavel Solodovnikov --- src/droid.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/droid.cpp b/src/droid.cpp index 7cc981ffbec..d672c9830d0 100644 --- a/src/droid.cpp +++ b/src/droid.cpp @@ -443,14 +443,16 @@ DROID::~DROID() if (psDroid->psGroup) { //free all droids associated with this Transporter - for (DROID* psCurr : psDroid->psGroup->psList) + mutating_list_iterate(psDroid->psGroup->psList, [psDroid](DROID* psCurr) { if (psCurr == psDroid) { - break; + return IterationResult::BREAK_ITERATION; } + // This will cause each droid to self-remove from `psGroup->psList`. delete psCurr; - } + return IterationResult::CONTINUE_ITERATION; + }); } }