startup: release the per-device D3D12 caches only when the module is unloaded #2582
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2590"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by thepatrickworld on 2026-11-25T22:13:12Z.
What / Why
Refs #2572. Two caches keep desktop D3D12 objects for a device for as long as the module that holds them is loaded: the indexed-validation pipeline (
startup_stubs/d3d12_draw.inl) and the rectangle-list argument converter (startup_stubs/rect_indirect_args.h). Both are function-local statics, so they are destroyed during the module's CRT teardown — and that teardown runs at two different moments.At process exit Windows has already detached the display driver, so releasing a pipeline state from those destructors reaches a stopped driver (
amdxc64, offset0x35f2a19, throughD3D12Core!CPipelineState::FinalRelease) and the loader's__fastfail(7)turns it into0xC0000409. That is what killed the fivestartup_indirect_*_hardwareruns.FreeLibraryunloads the module while the process and the driver are alive, and there every owned object must be released.Change
src/d3d12_xs_shim.cpp:DllMainrecords which detach is running —lpReservedis non-null only when the process is terminating, and the user entry runs before this module'satexittable, so the destructors see the flag.src/startup_stubs/d3d12_teardown.h: the flag, with the boundary written down, and why an inline variable is the right scope (each module that includes the caches keeps releasing normally).startup_stubs/d3d12_draw.inl,startup_stubs/rect_indirect_args.h: explicit destructors that detach their references on the terminating path and release them otherwise. Device keying is unchanged, as asked.src/startup_stubs/d3d12_xs_device.cpp:x2w_d3d12_cached_objects_probe, which warms both caches and reports how many objects they own for a device (5 when both are warm).tests/startup_teardown_unload.cpp+ two CMake entries (warp,hardware): loads the builtd3d12_xs.dllby path, warms both caches through the probe,FreeLibrary, and checks that the objects came back and the module is gone; a second load is left loaded so process termination exercises the other path.docs/experiments.md: one section, and the reproduction commands.Test
Built and run at
fb5f5e62onmain8f045aee, tree clean:Windows 11 25H2 26200.9974, MSVC 19.44.35230, AMD Radeon RX 9070 XT (
1002:7550, driver32.0.32029.2010),d3d12SDKLayers.dllabsent so no debug layer.startup_teardown_unload: passes on WARP and on the card.startup_indirect_*_hardwareruns now reach their last assertion and exit0(the executables' own status, run directly with theiradd_testarguments); all five died with0xC0000409before.translated_indexed*runs read-1073740791andrectreads0; without the converter destructorrectreads-1073740791and the four read0.Not covered
An explicit unload while a title still references the device, any adapter other than the one above, and the third static the issue names (
ComPtr<ID3D12Resource>pages ind3d12_xs_device.cpp:4103, cross-adapter placed-buffer path), which this patch does not touch. Logs and dumps stay local.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T22:52:26Z.
Built and run here: 2/2 pass, and four mutations of the teardown boundary are all caught — including the one that only changes when objects are released
The distinction this PR is about
d3d12_teardown.hseparates two detaches that look identical to a destructor:(
D3D12Core!CPipelineState::FinalRelease) and the loader's__fastfail(7)turns it into0xC0000409;FreeLibrary— the process and driver are alive, so every owned object must be released or it leaks for the restof the process.
DllMain'slpReservedis the discriminator, andsrc/d3d12_xs_shim.cpp:291sets the flag from it. That is aone-bool design, which means the failure mode of getting it backwards is a crash at exit or a silent leak — neither of
which an ordinary assertion catches. So I mutated the flag and both consumers.
d3d12_process_terminatinginitialisedtrue(never release, in either path)DllMainstops setting it (if (false) …)if (!terminating) return;→if (terminating) return;)All four are caught by the single fixture, on both adapters. The two guard inversions matter most: inverting one of them
leaves the other correct, so the fixture must be reaching both caches — the draw-side pipeline cache and
rect_indirect_args.h— which is what the header claims and what a "just check the process doesn't crash" test wouldmiss.
One thing the design gets right that is easy to miss
inline bool d3d12_process_terminatingis defined inline so each including module has its own copy. That means thetest executables, which include
rect_indirect_args.hwithout owning aDllMain, keep releasing normally — only the DLLthat owns the
DllMaincan silence its own teardown. The comment says as much and the fixture depends on it: if the flagwere a single extern, silencing it in the DLL would also silence the test's own caches and the
FreeLibraryhalf wouldstop testing anything.
Method note
My mutation run was interrupted mid-way (the third of four). I checked the tree before continuing rather than assuming:
git statusclean, and all four sites — the flag's initialiser, theDllMainassignment, and both guards — still intheir original form, with the draw guard still spelled
if (!…) return;. Only then did I re-run the fourth mutation. Theresult above is from a clean tree before and after.
AI-assisted; Windows 11 build 26300, WARP plus the installed physical GPU (Intel Arc 140T, driver 32.0.101.8132). Each
mutation rebuilt the target, ran the suite, reverted and rebuilt; final state 2/2 green and a clean working tree.