From 553952132f0654349b60b6a326a4add7b5d5367e Mon Sep 17 00:00:00 2001 From: BruebachL <44814898+BruebachL@users.noreply.github.com> Date: Thu, 27 Nov 2025 22:00:35 +0100 Subject: [PATCH] [Game] Fix CardZoneLogic::clearContents() (#6356) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Took 6 minutes Took 28 seconds Co-authored-by: Lukas BrĂ¼bach --- .../src/game/zones/logic/card_zone_logic.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/cockatrice/src/game/zones/logic/card_zone_logic.cpp b/cockatrice/src/game/zones/logic/card_zone_logic.cpp index b5869489a..a5dc64b50 100644 --- a/cockatrice/src/game/zones/logic/card_zone_logic.cpp +++ b/cockatrice/src/game/zones/logic/card_zone_logic.cpp @@ -148,17 +148,24 @@ void CardZoneLogic::moveAllToZone() void CardZoneLogic::clearContents() { - for (int i = 0; i < cards.size(); i++) { + // First gather the cards into a safe temporary list. + const CardList toClear = cards; + + // Detach and notify attached cards and zones *before* deleting anything. + for (CardItem *card : toClear) { // If an incorrectly implemented server doesn't return attached cards to whom they belong before dropping a // player, we have to return them to avoid a crash. - - const QList &attachedCards = cards[i]->getAttachedCards(); - for (auto attachedCard : attachedCards) { + const QList &attachedCards = card->getAttachedCards(); + for (CardItem *attachedCard : attachedCards) { emit attachedCard->getZone()->cardAdded(attachedCard); } - - player->deleteCard(cards.at(i)); } + + // Now request deletions after all manipulations are done. + for (CardItem *card : toClear) { + player->deleteCard(card); + } + cards.clear(); emit cardCountChanged(); }