Skip to content

Commit

Permalink
Fix: Crash due to drawRadarBlips calling removeMessage
Browse files Browse the repository at this point in the history
Because removeMessage may modify widgets, it cannot be called directly from within a widget event / callback (in this case, it was ultimately being called by RadarWidget::display).

Refactor this out to a new cleanupOldBeaconMessages() function which can be called at a safe time.
  • Loading branch information
past-due committed Feb 28, 2024
1 parent 3ddaccb commit 0bc7a84
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
2 changes: 2 additions & 0 deletions src/hci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1954,6 +1954,8 @@ void intDisplayWidgets()
{
desiredRadarVisibility = radarVisible();

cleanupOldBeaconMessages();

/* Ensure that any text messages are displayed at bottom of screen */
displayConsoleMessages();
}
Expand Down
35 changes: 0 additions & 35 deletions src/intdisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,6 @@ void drawRadarBlips(int radarX, int radarY, float pixSizeH, float pixSizeV, cons
{
UWORD imageID;
UDWORD delay = 150;
UDWORD i;
SDWORD width, height;
int x = 0, y = 0;
static const uint16_t imagesEnemy[] = {IMAGE_RAD_ENMREAD, IMAGE_RAD_ENM1, IMAGE_RAD_ENM2, IMAGE_RAD_ENM3};
Expand All @@ -1707,40 +1706,6 @@ void drawRadarBlips(int radarX, int radarY, float pixSizeH, float pixSizeV, cons
width = scrollMaxX - scrollMinX;
height = scrollMaxY - scrollMinY;

// Check if it's time to remove beacons
bool removedAMessage = false;
for (i = 0; i < MAX_PLAYERS; i++)
{
/* Go through all the proximity Displays*/
mutating_list_iterate(apsProxDisp[i], [&removedAMessage, i](PROXIMITY_DISPLAY* psProxDisp)
{
if (psProxDisp->psMessage->dataType == MSG_DATA_BEACON)
{
MESSAGE* psCurrMsg = psProxDisp->psMessage;
VIEWDATA* pViewData = psCurrMsg->pViewData;

ASSERT_OR_RETURN(IterationResult::CONTINUE_ITERATION, pViewData != nullptr, "Message without data!");

if (pViewData->type == VIEW_BEACON)
{
ASSERT_OR_RETURN(IterationResult::CONTINUE_ITERATION, pViewData->pData != nullptr, "Help message without data!");
if (pViewData->pData != nullptr && (((VIEW_PROXIMITY*)pViewData->pData)->timeAdded + 60000) <= gameTime)
{
debug(LOG_MSG, "blip timeout for %d, from %d", i, (((VIEW_PROXIMITY*)pViewData->pData)->sender));
removeMessage(psCurrMsg, i); //remove beacon
removedAMessage = true;
return IterationResult::BREAK_ITERATION; //there can only be 1 beacon per player
}
}
}
return IterationResult::CONTINUE_ITERATION;
});
}
if (removedAMessage)
{
jsDebugMessageUpdate();
}

/* Go through all the proximity Displays */
if (selectedPlayer < MAX_PLAYERS)
{
Expand Down
37 changes: 37 additions & 0 deletions src/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,3 +840,40 @@ void displayProximityMessage(PROXIMITY_DISPLAY *psProxDisp)
//set the read flag
psProxDisp->psMessage->read = true;
}

void cleanupOldBeaconMessages()
{
// Check if it's time to remove beacons
bool removedAMessage = false;
for (UDWORD i = 0; i < MAX_PLAYERS; i++)
{
/* Go through all the proximity Displays*/
mutating_list_iterate(apsProxDisp[i], [&removedAMessage, i](PROXIMITY_DISPLAY* psProxDisp)
{
if (psProxDisp->psMessage->dataType == MSG_DATA_BEACON)
{
MESSAGE* psCurrMsg = psProxDisp->psMessage;
VIEWDATA* pViewData = psCurrMsg->pViewData;

ASSERT_OR_RETURN(IterationResult::CONTINUE_ITERATION, pViewData != nullptr, "Message without data!");

if (pViewData->type == VIEW_BEACON)
{
ASSERT_OR_RETURN(IterationResult::CONTINUE_ITERATION, pViewData->pData != nullptr, "Help message without data!");
if (pViewData->pData != nullptr && (((VIEW_PROXIMITY*)pViewData->pData)->timeAdded + 60000) <= gameTime)
{
debug(LOG_MSG, "blip timeout for %d, from %d", i, (((VIEW_PROXIMITY*)pViewData->pData)->sender));
removeMessage(psCurrMsg, i); //remove beacon
removedAMessage = true;
return IterationResult::BREAK_ITERATION; //there can only be 1 beacon per player
}
}
}
return IterationResult::CONTINUE_ITERATION;
});
}
if (removedAMessage)
{
jsDebugMessageUpdate();
}
}
2 changes: 2 additions & 0 deletions src/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,6 @@ void displayProximityMessage(PROXIMITY_DISPLAY *psProxDisp);

bool messageInitVars();

void cleanupOldBeaconMessages();

#endif // __INCLUDED_SRC_MESSAGE_H__

0 comments on commit 0bc7a84

Please sign in to comment.