From 7d584c1f799d89e2aad805ac30b3f14bb3a6cad0 Mon Sep 17 00:00:00 2001 From: Antonio Martinez Date: Mon, 7 Oct 2024 16:59:51 -0700 Subject: [PATCH] Fix mobjcache memory leak after reloading gamestate Weird history splice from 711804be --- src/p_local.h | 1 + src/p_mobj.c | 29 +++++++++++++++++++---------- src/p_saveg.cpp | 4 ++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/p_local.h b/src/p_local.h index 4d2bbdb07..e886ef695 100644 --- a/src/p_local.h +++ b/src/p_local.h @@ -258,6 +258,7 @@ mobjtype_t P_GetMobjtype(UINT16 mthingtype); void P_RespawnSpecials(void); fixed_t P_GetMobjDefaultScale(mobj_t *mobj); +mobj_t *P_AllocateMobj(void); mobj_t *P_SpawnMobj(fixed_t x, fixed_t y, fixed_t z, mobjtype_t type); void P_CalculatePrecipFloor(precipmobj_t *mobj); diff --git a/src/p_mobj.c b/src/p_mobj.c index e6b683fc3..6a48a661c 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -10844,6 +10844,24 @@ static void P_DefaultMobjShadowScale(mobj_t *thing) } } +mobj_t *P_AllocateMobj(void) +{ + mobj_t *mobj; + + if (mobjcache != NULL) + { + mobj = mobjcache; + mobjcache = mobjcache->hnext; + memset(mobj, 0, sizeof(*mobj)); + } + else + { + mobj = Z_Calloc(sizeof (*mobj), PU_LEVEL, NULL); + } + + return mobj; +} + // // P_SpawnMobj // @@ -10870,16 +10888,7 @@ mobj_t *P_SpawnMobj(fixed_t x, fixed_t y, fixed_t z, mobjtype_t type) type = MT_RAY; } - if (mobjcache != NULL) - { - mobj = mobjcache; - mobjcache = mobjcache->hnext; - memset(mobj, 0, sizeof(*mobj)); - } - else - { - mobj = Z_Calloc(sizeof (*mobj), PU_LEVEL, NULL); - } + mobj = P_AllocateMobj(); // this is officially a mobj, declared as soon as possible. mobj->thinker.function.acp1 = (actionf_p1)P_MobjThinker; diff --git a/src/p_saveg.cpp b/src/p_saveg.cpp index a34d6d783..57536c4a3 100644 --- a/src/p_saveg.cpp +++ b/src/p_saveg.cpp @@ -4511,13 +4511,13 @@ static thinker_t* LoadMobjThinker(savebuffer_t *save, actionf_p1 thinker) return NULL; } - mobj = (mobj_t*)Z_Calloc(sizeof (*mobj), PU_LEVEL, NULL); + mobj = P_AllocateMobj(); mobj->spawnpoint = &mapthings[spawnpointnum]; mapthings[spawnpointnum].mobj = mobj; } else - mobj = (mobj_t*)Z_Calloc(sizeof (*mobj), PU_LEVEL, NULL); + mobj = P_AllocateMobj(); // declare this as a valid mobj as soon as possible. mobj->thinker.function.acp1 = thinker;