Skip to content

Commit 9079b36

Browse files
OverlordContain: Fix iterator invalidation in removeAllRedirectedContained
1 parent 04364a5 commit 9079b36

2 files changed

Lines changed: 12 additions & 16 deletions

File tree

Generals/Code/GameEngine/Source/GameLogic/Object/Contain/OverlordContain.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,13 @@ void OverlordContain::removeAllContained( Bool exposeStealthUnits )
254254
return;
255255
}
256256

257-
const ContainedItemsList *fullList = getRedirectedContain()->getContainedItemsList();
258-
259-
Object *obj;
260-
ContainedItemsList::const_iterator it;
261-
it = (*fullList).begin();
262-
while( it != (*fullList).end() )
257+
// Use a safe iterator pattern to avoid iterator invalidation.
258+
// Always get a fresh iterator from the beginning after each removal,
259+
// similar to OpenContain::removeAllContained()
260+
const ContainedItemsList *fullList;
261+
while( (fullList = getRedirectedContain()->getContainedItemsList()) != NULL && !fullList->empty() )
263262
{
264-
obj = *it;
265-
it++;
263+
Object *obj = fullList->front();
266264
removeFromContain( obj, exposeStealthUnits );
267265
}
268266
}

GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/OverlordContain.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,13 @@ void OverlordContain::removeAllContained( Bool exposeStealthUnits )
328328
return;
329329
}
330330

331-
const ContainedItemsList *fullList = getRedirectedContain()->getContainedItemsList();
332-
333-
Object *obj;
334-
ContainedItemsList::const_iterator it;
335-
it = (*fullList).begin();
336-
while( it != (*fullList).end() )
331+
// Use a safe iterator pattern to avoid iterator invalidation.
332+
// Always get a fresh iterator from the beginning after each removal,
333+
// similar to OpenContain::removeAllContained()
334+
const ContainedItemsList *fullList;
335+
while( (fullList = getRedirectedContain()->getContainedItemsList()) != NULL && !fullList->empty() )
337336
{
338-
obj = *it;
339-
it++;
337+
Object *obj = fullList->front();
340338
removeFromContain( obj, exposeStealthUnits );
341339
}
342340
}

0 commit comments

Comments
 (0)