From 86ddec3f76a6f19b45f61b6b48859bca7effcf49 Mon Sep 17 00:00:00 2001 From: smallmodel <15067410+smallmodel@users.noreply.github.com> Date: Sun, 8 Sep 2024 00:56:03 +0200 Subject: [PATCH] Clear the world to prevent crashes --- code/renderer/tr_bsp.c | 24 ++++++++++++++++++++++++ code/renderer/tr_init.c | 7 +++++++ code/renderer/tr_local.h | 1 + code/renderer/tr_marks_permanent.c | 10 ++++------ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/code/renderer/tr_bsp.c b/code/renderer/tr_bsp.c index 7d01b47a..7362269d 100644 --- a/code/renderer/tr_bsp.c +++ b/code/renderer/tr_bsp.c @@ -2432,11 +2432,23 @@ void RE_LoadWorldMap( const char *name ) { UI_LoadResource("*116"); } +/* +================= +RE_MapVersion + +================= +*/ int RE_MapVersion(void) { return map_version; } +/* +================= +RE_PrintBSPFileSizes + +================= +*/ void RE_PrintBSPFileSizes(void) { ri.Printf(PRINT_ALL, "%s: %i\n", s_worldData.name, map_length); @@ -2466,3 +2478,15 @@ void RE_PrintBSPFileSizes(void) ri.Printf(PRINT_ALL, "%6i static models defs %7i\n", g_nStaticModels, 164 * g_nStaticModels); ri.Printf(PRINT_ALL, " static model indexes %7i\n", g_nStaticModelIndices); } + +/* +================= +R_ClearWorld + +Set the world to NULL to prevent anyone from accessing +freed world data +================= +*/ +void R_ClearWorld(void) { + tr.world = NULL; +} diff --git a/code/renderer/tr_init.c b/code/renderer/tr_init.c index d9f5d235..4b5d7b0a 100644 --- a/code/renderer/tr_init.c +++ b/code/renderer/tr_init.c @@ -1583,6 +1583,13 @@ void RE_BeginRegistration(glconfig_t* glconfigOut) { tr.viewCluster = -1; // force markleafs to regenerate R_ClearFlares(); RE_ClearScene(); + + // Added in OPM + // Clear the world because some components + // like the mark system access the world object + // to free allocated marks + R_ClearWorld(); + R_SetupShaders(); R_InitLensFlare(); R_LevelMarksInit(); diff --git a/code/renderer/tr_local.h b/code/renderer/tr_local.h index 210f5f51..2654a61d 100644 --- a/code/renderer/tr_local.h +++ b/code/renderer/tr_local.h @@ -1757,6 +1757,7 @@ void RE_SetWorldVisData( const byte *vis ); qhandle_t RE_RegisterModel( const char *name ); qhandle_t RE_RegisterSkin( const char *name ); void RE_Shutdown( qboolean destroyWindow ); +void R_ClearWorld(void); qboolean R_GetEntityToken( char *buffer, int size ); diff --git a/code/renderer/tr_marks_permanent.c b/code/renderer/tr_marks_permanent.c index 256ba4c2..d2f1ad2d 100644 --- a/code/renderer/tr_marks_permanent.c +++ b/code/renderer/tr_marks_permanent.c @@ -1593,12 +1593,10 @@ void R_LevelMarksFree() for (i = tr.world->numDecisionNodes; i < tr.world->numnodes; i++) { mnode_t *pLeaf = &tr.world->nodes[i]; - if (pLeaf->iNumMarkFragment) { - if (pLeaf->pFirstMarkFragment) { - ri.Free(pLeaf->pFirstMarkFragment); - pLeaf->pFirstMarkFragment = NULL; - pLeaf->iNumMarkFragment = 0; - } + if (pLeaf->iNumMarkFragment && pLeaf->pFirstMarkFragment) { + ri.Free(pLeaf->pFirstMarkFragment); + pLeaf->pFirstMarkFragment = NULL; + pLeaf->iNumMarkFragment = 0; } } }