Repository navigation
dxgi: the factory made from a Vulkan device holds that device - #8569
arshazam1387 wants to merge 2 commits into
Conversation
rt::dxgi::Factory keeps the device's VulkanContext and Vulkan handles but held no reference to the device, so an app that released its device before the factory (or an adapter from it) left the factory on freed memory. create_factory now takes the owning object; the factory AddRefs it and releases it in its destructor. create_dxgi_factory passes the device behind the Xbox-ABI proxy. New dxgi_factory_lifetime test (Vulkan GPU, exits 77 without one). AI-assisted.
|
Tested
AI-assisted. |
|
Tested
|
wavehs
left a comment
There was a problem hiding this comment.
Read the lifetime change and the whole reference graph; the fix is right and I could not find a cycle or a new dangling path.
The graph, as it stands after this PR
Factoryholdsowner_(ID3D12Device*, +1) and the raw Vulkan handles.create_dxgi_factorypasses the inner device (device_proxy_real_device), so a title that holds the Xbox-ABI proxy gets a factory on the real device.AdapterAddRefs itsIDXGIFactory4 parent_(dxgi_com.cpp:1089) and releases it in its destructor, so an adapter keeps the factory, and through it the device.- Nothing on the device side keeps a factory:
set_device_adapter_provider(d3d12_vulkan_device.cpp:8360) creates a factory, enumerates one adapter and releases the factory in the same call;Device::outer_(:8325) is a raw pointer documented as owned by the proxy;Child::device_(d3d12_com_object.h:169) points at the proxy or the device, never at a factory. Sodevice -> factory -> devicedoes not exist, and the factory's own reference is the only new edge. - The one path that could have made a cycle — a title's
IDXGIDevice1::GetAdapterhanding back an adapter that the proxy cached — does not: the provider's adapter is returned to the caller and nothing stores it on the proxy.
The destructor ordering. ~Factory() releases the owner in the body, before private_data_ is destroyed, so a SetPrivateDataInterface object is released after the device may have been deleted. That is safe for D3D12 objects because a Child holds a reference to its device (Child::device_, attach_device), so any resource, heap, queue or root signature in the private data keeps the device alive on its own. It is only unsafe for an arbitrary non-device object that reaches the Vulkan handles without a device reference, which the layer does not hand out. Worth a sentence in the comment on ~Factory so nobody "fixes" it by moving the release to a member later.
Two notes for the follow-up, not this PR
create_factoryAddRefsownereven whenQueryInterface(riid, out)fails;factory->Release()then releases the owner insidedelete this. Balanced, and~Factorytouches no member after the release, so it is fine — but it does mean~Factorycan run arbitrary device teardown inside the factory's ownRelease, which is why nothing may be added to the destructor body after the owner release.- The
~SwapChainordering you call out (D3D-818 / #8176) is real and unchanged by this PR; with this change the device is freed by the factory release rather than by the queue release, so #8176's fix should indeed destroy the swap chain before both.
I could not run dxgi_factory_lifetime here (Windows-only, no Vulkan device on this machine), so the CPU side is a code read only; the test's refcount arithmetic and the raw->Release() == 1 expectation match the change.
AI-assisted. macOS 27.2.1 arm64, no GPU; no game files.
|
Tested
AI-assisted (Claude Code). |
|
Tested 0611b13 (merged onto main 867d922) on RTX 5070, NVIDIA 621.24, Windows 11 (build 26200), Vulkan validation layer on (
Ran the tests that belong to the test files this PR changes (ctest entries, or the test program directly when it is not registered), with no game files ( |
Claimed in the coordination issue: #8186 (comment)
What and why
rt::dxgi::Factory(runtime/shims/dxgi_com.cpp) keeps the device'sVulkanContext&and its Vulkan handles(instance, physical device,
vkGetDeviceProcAddr, queue), taken inrt::d3d12::create_dxgi_factory, but held noreference to the D3D12 device. An app that releases its device before the factory, or before an adapter from it
(an adapter holds its factory), leaves the factory on freed memory:
EnumAdapters*,EnumAdapterByLuidand thetearing probe in
CheckFeatureSupportthen use a destroyed instance and device. A standalone device owns itsinstance, so this is a real use-after-free, not only a dangling reference.
Change
rt::dxgi::create_factorytakes the object that owns the context (IUnknown* owner); the factory AddRefs it andreleases it in its destructor.
create_dxgi_factorypasses the device behind the Xbox-ABI proxy (the proxy holds its own reference to it, and theadapter provider in
set_device_outeralready works on that device).and releases it in the same call.
dxgi_device_test.cppupdated (the adapter keeps the device behind the proxy alive now, notthe proxy).
Not in scope:
~SwapChainreleases its queue and factory before itsVulkanSwapchainmember is destroyed, so a swapchain released last can still tear down on a freed device. That ordering is D3D-818 (#8176); with this change the
device is freed by the factory release instead of the queue release, so #8176's fix should destroy the swap chain
before both releases.
Tests
New
dxgi_factory_lifetime(Vulkan GPU, exits 77 without one): the factory takes exactly one device reference andgives it back; the factory works after the app's last device reference is gone (the app's
Releasereturns 1); anadapter works after both the device and the factory are released. Without the fix the first two cases fail on the
reference counts (by reading the code; not run against unfixed main).
Windows 11, NVIDIA GeForce RTX 5070 Laptop GPU (driver 32.0.16.1714), RelWithDebInfo:
ctest -R "swapchain|present|dxgi": all pass,dxgi_factory_lifetimeincluded.(
d3d12_vulkan_device,d3d12_texture_tiles,d3d12_indirect_dispatch_count,d3d12_depth_resolve,d3d12_heap_properties,d3d12_pipeline_library_host);d3d12_bindless_churn(_validated)failed once under-j 4with other GPU work running and pass on their own.python -I scripts/check_no_game_data.pyon the changed files: clean.Rebased on main 867d922 and re-run: build clean,
ctest -R "swapchain|present|dxgi"9/9 pass.No game data. AI-assisted.