Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions Generals/Code/GameEngine/Source/GameClient/GameClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,17 +757,32 @@ void GameClient::updateHeadless()
*/
void GameClient::iterateDrawablesInRegion( Region3D *region, GameClientFuncPtr userFunc, void *userData )
{
Drawable *draw, *nextDrawable;

for( draw = m_drawableList; draw; draw=nextDrawable )
// TheSuperHackers @bugfix Collect matching drawable IDs upfront before invoking any callbacks.
// The userFunc (e.g. drawablePostDraw) can indirectly call getShroudedStatus(), which may
// trigger ghost-object snapshot operations that destroy drawables and remove them from
// m_drawableList. Pre-fetching nextDrawable before the call is not sufficient because the
// pre-fetched pointer itself can become dangling if that drawable is also destroyed during
// the callback. By collecting IDs first and then validating each via findDrawableByID(),
// we ensure we never dereference a freed drawable pointer.
static std::vector<DrawableID> s_drawableIDs;
s_drawableIDs.clear();

for( Drawable *draw = m_drawableList; draw; draw = draw->getNextDrawable() )
{
nextDrawable = draw->getNextDrawable();

Coord3D pos = *draw->getPosition();
if( region == nullptr ||
(pos.x >= region->lo.x && pos.x <= region->hi.x &&
pos.y >= region->lo.y && pos.y <= region->hi.y &&
pos.z >= region->lo.z && pos.z <= region->hi.z) )
{
s_drawableIDs.push_back( draw->getID() );
}
}

for( Int i = 0; i < (Int)s_drawableIDs.size(); ++i )
{
Drawable *draw = findDrawableByID( s_drawableIDs[i] );
if( draw != nullptr )
{
(*userFunc)( draw, userData );
}
Expand Down
25 changes: 20 additions & 5 deletions GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,17 +825,32 @@ void GameClient::updateHeadless()
*/
void GameClient::iterateDrawablesInRegion( Region3D *region, GameClientFuncPtr userFunc, void *userData )
{
Drawable *draw, *nextDrawable;

for( draw = m_drawableList; draw; draw=nextDrawable )
// TheSuperHackers @bugfix Collect matching drawable IDs upfront before invoking any callbacks.
// The userFunc (e.g. drawablePostDraw) can indirectly call getShroudedStatus(), which may
// trigger ghost-object snapshot operations that destroy drawables and remove them from
// m_drawableList. Pre-fetching nextDrawable before the call is not sufficient because the
// pre-fetched pointer itself can become dangling if that drawable is also destroyed during
// the callback. By collecting IDs first and then validating each via findDrawableByID(),
// we ensure we never dereference a freed drawable pointer.
static std::vector<DrawableID> s_drawableIDs;
s_drawableIDs.clear();

for( Drawable *draw = m_drawableList; draw; draw = draw->getNextDrawable() )
{
nextDrawable = draw->getNextDrawable();

Coord3D pos = *draw->getPosition();
if( region == nullptr ||
(pos.x >= region->lo.x && pos.x <= region->hi.x &&
pos.y >= region->lo.y && pos.y <= region->hi.y &&
pos.z >= region->lo.z && pos.z <= region->hi.z) )
{
s_drawableIDs.push_back( draw->getID() );
}
}

for( Int i = 0; i < (Int)s_drawableIDs.size(); ++i )
{
Drawable *draw = findDrawableByID( s_drawableIDs[i] );
if( draw != nullptr )
{
(*userFunc)( draw, userData );
}
Expand Down
Loading