Skip to content

Free the previous scratch trace model in SetupTrmModel - #73

Merged
themuffinator merged 1 commit into
themuffinator:mainfrom
danperisse-hue:fix-trm-model-leak
Nov 27, 2026
Merged

themuffinator merged 1 commit into
themuffinator:mainfrom
danperisse-hue:fix-trm-model-leak

Conversation

@danperisse-hue

Copy link
Copy Markdown
Contributor

PREY's game code calls collisionModelManager->SetupTrmModel for trace-model clip models every frame (idClipModel::Handle, reached from SetWaterLevel, CheckGround and the weapon traces, and Clip.cpp's contact queries) and never frees what it gets back: Doom 3's manager refilled one scratch model. The shim in CollisionModel.h calls ModelFromTrm, which builds a new standalone model on each call, so every call leaked one.

This keeps one scratch model, as Doom 3 did, and frees the previous one before making the next.

Evidence (Linux x64, GL, six laps of six maps)

A sampling allocation tracer (LD_PRELOAD, symbolized with addr2line) put 40-43 MB per lap on ModelFromTrm under idClipModel::Handle, plus about 8 MB in AddPolygonToNode / AddBrushToNode / AllocNode for the same models.

Build RSS lap 1 → 6 (MB) Growth Heap growth
main (two runs) 1320 → 1725, 1325 → 1735 +405, +410 +262, +254
#71 only 1253 → 1535 +282 +280
this change only 1302 → 1556 +254 +70
#71 + this change 1236 → 1311 +75 +61

All 36 map loads succeeded in every run.

Test

collision_trm_scratch_model checks that the shim keeps one static scratch model and frees it before making the next, that Clip.cpp still calls SetupTrmModel without freeing the result, and that the check rejects the old shim. It passes on Windows and WSL, together with suite_status_contract, docs_link_integrity and source_charset_integrity.

PREY's game code calls collisionModelManager->SetupTrmModel for trace-model
clip models every frame (idClipModel::Handle, reached from SetWaterLevel,
CheckGround and the weapon traces, and Clip.cpp's contact queries) and never
frees what it gets back: Doom 3's manager refilled one scratch model. The
shim calls ModelFromTrm, which builds a new standalone model on each call,
so every call leaked one.

On Linux x64 a sampling allocation tracer put 40-43 MB per lap of six maps
on ModelFromTrm under idClipModel::Handle, plus about 8 MB in
AddPolygonToNode, AddBrushToNode and AllocNode for the same models. Over six
laps (GL), heap growth went from +262 and +254 MB on main to +70 MB with
this change. With themuffinator#71 applied too, resident memory grew by 75 MB instead of
about 405 MB.

collision_trm_scratch_model checks that the shim keeps one scratch model
and frees the previous one before making the next, that Clip.cpp still
calls SetupTrmModel without freeing the result, and that the check rejects
the old shim.
@danperisse-hue

Copy link
Copy Markdown
Contributor Author

Checked against #72, since both touch collision-model lifetimes: they don't overlap.

@themuffinator
themuffinator merged commit dd3b221 into themuffinator:main Nov 27, 2026
9 checks passed
themuffinator added a commit that referenced this pull request Nov 29, 2026
…round, and stop rewriting the config at launch

Ported from openPR06, the PREY port built on openQ4 (third round: pull
requests #53-#81 and issues #61-#78 by danperisse-hue).

- A deflated PK4 member keeps what it inflates once it is seeked (up to
  32 MiB), so a wave or model chunk scan no longer inflates the member again
  at every seek, and a seek to the current position does nothing (#53).
- Generated animation caches are built in memory and written, synced and
  closed by a writer thread, at most 16 files and 64 MiB queued; the main
  thread promotes them, and game shutdown waits for the queue (#54, as
  adapted to openQ4's version-3 cache in openPR06 a945a6b).
- idInternalCVar::Update flags CVAR_ARCHIVE only when the declaration makes
  a cvar archived, so a module registering archived cvars the config already
  set no longer rewrites openQ4Config.cfg on every launch (#63).
- idProgram::FindFreeResultDef finds a reusable <RESULT> temporary through a
  per-scope hash chain, newest first like the def list, instead of walking
  every result compiled so far (#64).
- Linux: the loose-directory memo runs on case-sensitive filesystems too,
  resolving case once per directory (#56); glibc malloc arenas are capped at
  two unless MALLOC_ARENA_MAX is set (#71); the fatal-breadcrumb directory is
  created like mkdir -p (#57).
- com_timestampPrints describes 1 as seconds and 2 as milliseconds (#60).
- Tests: filesystem_length_probe_contract follows the memo (#65),
  posix_memory_management checks the arena cap, and android_savepath
  extracts Posix_MkdirParents (openPR06 bd071e1).
- BUILDING.md: running the Linux stock smokes under a headless Weston (#81).

Prey-only or already in openQ4, and not ported: #48, #55, #59, #66-#68,
#70, #73, #74, #76, #77, #79, #80.

Co-authored-by: danperisse-hue <danperisse@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants