From 1ce8ceac5d72458fdfbb9cda2a874f62b6f20ea0 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Mon, 2 Oct 2023 19:03:27 +0200 Subject: [PATCH] fixed GC handling of players. These were missing null checks, the player pointers also weren't nulled after deleting the objects. --- source/core/gamecontrol.cpp | 6 ++++- source/games/blood/src/blood.cpp | 16 +++++++----- source/games/duke/src/game.cpp | 26 +++++++++++-------- source/games/exhumed/src/player.cpp | 20 ++++++++------ source/games/sw/src/game.cpp | 20 ++++++++------ .../static/zscript/games/blood/bloodgame.zs | 3 ++- wadsrc/static/zscript/games/sw/swgame.zs | 4 +-- 7 files changed, 58 insertions(+), 37 deletions(-) diff --git a/source/core/gamecontrol.cpp b/source/core/gamecontrol.cpp index be66b38f5..f0e03e801 100644 --- a/source/core/gamecontrol.cpp +++ b/source/core/gamecontrol.cpp @@ -635,7 +635,11 @@ int GameMain() if (gi) { gi->FreeLevelData(); - for (int i = 0; i < MAXPLAYERS; i++) delete PlayerArray[i]; + for (int i = 0; i < MAXPLAYERS; i++) + { + delete PlayerArray[i]; + PlayerArray[i] = nullptr; + } } DestroyAltHUD(); DeinitMenus(); diff --git a/source/games/blood/src/blood.cpp b/source/games/blood/src/blood.cpp index 56f23a072..97243b58a 100644 --- a/source/games/blood/src/blood.cpp +++ b/source/games/blood/src/blood.cpp @@ -98,12 +98,16 @@ static void markgcroots() MarkSprInSect(); for (int i = 0; i < MAXPLAYERS; i++) { - GC::Mark(getPlayer(i)->actor); - GC::MarkArray(getPlayer(i)->ctfFlagState, 2); - GC::Mark(getPlayer(i)->aimTarget); - GC::MarkArray(getPlayer(i)->aimTargets, 16); - GC::Mark(getPlayer(i)->fragger); - GC::Mark(getPlayer(i)->voodooTarget); + auto plr = getPlayer(i); + if (plr) + { + GC::Mark(plr->actor); + GC::MarkArray(plr->ctfFlagState, 2); + GC::Mark(plr->aimTarget); + GC::MarkArray(plr->aimTargets, 16); + GC::Mark(plr->fragger); + GC::Mark(plr->voodooTarget); + } } for (auto& evobj : rxBucket) { diff --git a/source/games/duke/src/game.cpp b/source/games/duke/src/game.cpp index ed35f07e0..723368cce 100644 --- a/source/games/duke/src/game.cpp +++ b/source/games/duke/src/game.cpp @@ -85,18 +85,22 @@ static void markgcroots() GC::Mark(ud.cameraactor); for (int i = 0; i < MAXPLAYERS; i++) { - GC::Mark(getPlayer(i)->actor); - GC::Mark(getPlayer(i)->actorsqu); - GC::Mark(getPlayer(i)->wackedbyactor); - GC::Mark(getPlayer(i)->on_crane); - GC::Mark(getPlayer(i)->holoduke_on); - GC::Mark(getPlayer(i)->somethingonplayer); - GC::Mark(getPlayer(i)->access_spritenum); - GC::Mark(getPlayer(i)->dummyplayersprite); - GC::Mark(getPlayer(i)->newOwner); - for (auto& var : getPlayer(i)->uservars) + auto plr = getPlayer(i); + if (plr) { - var.Mark(); + GC::Mark(plr->actor); + GC::Mark(plr->actorsqu); + GC::Mark(plr->wackedbyactor); + GC::Mark(plr->on_crane); + GC::Mark(plr->holoduke_on); + GC::Mark(plr->somethingonplayer); + GC::Mark(plr->access_spritenum); + GC::Mark(plr->dummyplayersprite); + GC::Mark(plr->newOwner); + for (auto& var : plr->uservars) + { + var.Mark(); + } } } } diff --git a/source/games/exhumed/src/player.cpp b/source/games/exhumed/src/player.cpp index 44b364660..458a7d632 100644 --- a/source/games/exhumed/src/player.cpp +++ b/source/games/exhumed/src/player.cpp @@ -83,16 +83,20 @@ int nCurStartSprite; size_t MarkPlayers() { - for (int i = 0; i < kMaxWeapons; i++) + for (int i = 0; i < MAXPLAYERS; i++) { - GC::Mark(getPlayer(i)->actor); - GC::Mark(getPlayer(i)->pDoppleSprite); - GC::Mark(getPlayer(i)->pPlayerFloorSprite); - GC::Mark(getPlayer(i)->pPlayerGrenade); - GC::Mark(getPlayer(i)->pTarget); + auto plr = getPlayer(i); + if (plr) + { + GC::Mark(plr->actor); + GC::Mark(plr->pDoppleSprite); + GC::Mark(plr->pPlayerFloorSprite); + GC::Mark(plr->pPlayerGrenade); + GC::Mark(plr->pTarget); + } } - GC::MarkArray(nNetStartSprite, kMaxPlayers); - return 6 * kMaxPlayers; + GC::MarkArray(nNetStartSprite, MAXPLAYERS); + return 6 * MAXPLAYERS; } //--------------------------------------------------------------------------- diff --git a/source/games/sw/src/game.cpp b/source/games/sw/src/game.cpp index 9aab8af4f..1a8fc104c 100644 --- a/source/games/sw/src/game.cpp +++ b/source/games/sw/src/game.cpp @@ -120,14 +120,18 @@ void markgcroots() GC::MarkArray(BossSpriteNum, 3); for (int i = 0; i < MAXPLAYERS; i++) { - GC::Mark(getPlayer(i)->actor); - GC::Mark(getPlayer(i)->lowActor); - GC::Mark(getPlayer(i)->highActor); - GC::Mark(getPlayer(i)->remoteActor); - GC::Mark(getPlayer(i)->PlayerUnderActor); - GC::Mark(getPlayer(i)->KillerActor); - GC::Mark(getPlayer(i)->HitBy); - GC::Mark(getPlayer(i)->last_camera_act); + auto plr = getPlayer(i); + if (plr) + { + GC::Mark(plr->actor); + GC::Mark(plr->lowActor); + GC::Mark(plr->highActor); + GC::Mark(plr->remoteActor); + GC::Mark(plr->PlayerUnderActor); + GC::Mark(plr->KillerActor); + GC::Mark(plr->HitBy); + GC::Mark(plr->last_camera_act); + } } for (auto& so : SectorObject) { diff --git a/wadsrc/static/zscript/games/blood/bloodgame.zs b/wadsrc/static/zscript/games/blood/bloodgame.zs index 2b83635a8..6308b1e23 100644 --- a/wadsrc/static/zscript/games/blood/bloodgame.zs +++ b/wadsrc/static/zscript/games/blood/bloodgame.zs @@ -71,6 +71,8 @@ struct PACKINFO // not native! struct BloodPlayer native { + native uint8 pnum; // Connect id + native int GetHealth(); // health is stored in the XSPRITE which cannot be safely exported to scripting at the moment due to pending refactoring. native int powerupCheck(int pwup); //DUDEINFO* pDudeInfo; @@ -89,7 +91,6 @@ struct BloodPlayer native native int swayAmp; native double swayHeight; native double swayWidth; - native int nPlayer; // Connect id //native int nSprite; native int lifeMode; native double zView; diff --git a/wadsrc/static/zscript/games/sw/swgame.zs b/wadsrc/static/zscript/games/sw/swgame.zs index e95ccb033..aef9a3d37 100644 --- a/wadsrc/static/zscript/games/sw/swgame.zs +++ b/wadsrc/static/zscript/games/sw/swgame.zs @@ -147,6 +147,8 @@ struct SW native struct SWPlayer native { + native uint8 pnum; // carry along the player number + // variable that fit in the sprite or user structure /* union @@ -217,8 +219,6 @@ struct SWPlayer native native double RevolveDeltaAng; - native int16 pnum; // carry along the player number - //native int16 LadderSector; native int16 JumpDuration; native int16 WadeDepth;