From 9d135c745b8951f062dcd9825514a872a67581b8 Mon Sep 17 00:00:00 2001 From: Mauller <26652186+Mauller@users.noreply.github.com> Date: Thu, 26 Jun 2025 19:12:04 +0100 Subject: [PATCH] [GEN][ZH] Fix game crash in AIGroup::groupSell() --- .../Source/GameLogic/AI/AIGroup.cpp | 24 +++++++++---------- .../Source/GameLogic/AI/AIGroup.cpp | 24 +++++++++---------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/Generals/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp b/Generals/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp index ad12247a65f..5699e5c0e85 100644 --- a/Generals/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp +++ b/Generals/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp @@ -2722,23 +2722,21 @@ void AIGroup::groupCheer( CommandSourceType cmdSource ) */ void AIGroup::groupSell( CommandSourceType cmdSource ) { - std::list::iterator i, thisIterator; - Object *obj; + std::list::iterator i; + std::vector groupObjectsCopy; + groupObjectsCopy.reserve(m_memberListSize); + // TheSuperHackers @bugfix Mauller 26/06/2025 when sellObject is called, the member list objects in this AIGroup get removed from it. This happens within the Object::deselectObject() function. + // This deletes the local AIGroup object from under the call to groupSell, therefore we need to make a local copy of the objects that need selling. for( i = m_memberList.begin(); i != m_memberList.end(); /*empty*/ ) { + groupObjectsCopy.push_back( *i++ ); + } - // work off of 'thisIterator' as we may change the contents of this list - thisIterator = i; - ++i; - - // get object - obj = *thisIterator; - - // try to sell object - TheBuildAssistant->sellObject( obj ); - - } // end for, i + for( size_t j = 0; j < groupObjectsCopy.size(); ++j ) + { + TheBuildAssistant->sellObject( groupObjectsCopy[j] ); + } } diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp index a793feceb3f..fcadb38c6a8 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp @@ -2795,23 +2795,21 @@ void AIGroup::groupCheer( CommandSourceType cmdSource ) */ void AIGroup::groupSell( CommandSourceType cmdSource ) { - std::list::iterator i, thisIterator; - Object *obj; + std::list::iterator i; + std::vector groupObjectsCopy; + groupObjectsCopy.reserve(m_memberListSize); + // TheSuperHackers @bugfix Mauller 26/06/2025 when sellObject is called, the member list objects in this AIGroup get removed from it. This happens within the Object::deselectObject() function. + // This deletes the local AIGroup object from under the call to groupSell, therefore we need to make a local copy of the objects that need selling. for( i = m_memberList.begin(); i != m_memberList.end(); /*empty*/ ) { + groupObjectsCopy.push_back( *i++ ); + } - // work off of 'thisIterator' as we may change the contents of this list - thisIterator = i; - ++i; - - // get object - obj = *thisIterator; - - // try to sell object - TheBuildAssistant->sellObject( obj ); - - } // end for, i + for( size_t j = 0; j < groupObjectsCopy.size(); ++j ) + { + TheBuildAssistant->sellObject( groupObjectsCopy[j] ); + } }