fix(agc): release pooled buffers and image mirrors before the device #849

Merged
meh7an merged 1 commit from upstream-pr/849 into main 2026-10-07 02:26:11 +00:00
meh7an commented 2026-10-06 09:37:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by meh7an on 2026-10-06T09:37:52Z.


What

Closing a title crashes in the device teardown: Driver::Shutdown destroys the VulkanDevice, State::~State calls vkDestroyDevice, and only then are the members it still holds destroyed. One of them is patternBuffers, the cache of non-uniform fill pattern buffers. Their DeviceBuffer destructors then destroy buffers and free memory of a device that no longer exists.

Two other holders keep buffers of the device past vkDestroyDevice in the same way:

  • copiedWriters, the ShaderResources of dispatches whose copied buffers are listed, is also a State member.
  • The image mirrors (Mirrors() in GuestBufferMemory.cpp) are a function-local static, so their buffers live until static destruction.

The teardown now clears copiedWriters before the resource cache, and drops the image mirrors and the pattern buffers right after the cached textures, while the buffer pool and the device still exist. ClearImageMirrors empties the registry for the given device and destroys the mirrors after releasing its lock.

Tested

Windows 11, MinGW GCC 15.2, RTX 2080 Ti, Sonic Origins Plus (PPSA05324), closing the window with WM_CLOSE on the main menu:

  • Without this change the process ends with 0xc0000005 in nvoglv64 under DeviceBuffer::release <- DeviceBuffer::~DeviceBuffer <- VulkanDevice::~VulkanDevice <- Driver::Shutdown, called from the VideoOut thread.
  • With it the process exits with code 0 about 3 s after WM_CLOSE, without a crash report.

Both runs used my working branch (this change on top of other driver work). This branch builds on Windows and the 165 agc driver, recompiler and execution test targets pass.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4756895946 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/849). Originally posted by [meh7an](https://github.com/meh7an) on 2026-10-06T09:37:52Z. --- ### What Closing a title crashes in the device teardown: `Driver::Shutdown` destroys the `VulkanDevice`, `State::~State` calls `vkDestroyDevice`, and only then are the members it still holds destroyed. One of them is `patternBuffers`, the cache of non-uniform fill pattern buffers. Their `DeviceBuffer` destructors then destroy buffers and free memory of a device that no longer exists. Two other holders keep buffers of the device past `vkDestroyDevice` in the same way: - `copiedWriters`, the `ShaderResources` of dispatches whose copied buffers are listed, is also a `State` member. - The image mirrors (`Mirrors()` in GuestBufferMemory.cpp) are a function-local static, so their buffers live until static destruction. The teardown now clears `copiedWriters` before the resource cache, and drops the image mirrors and the pattern buffers right after the cached textures, while the buffer pool and the device still exist. `ClearImageMirrors` empties the registry for the given device and destroys the mirrors after releasing its lock. ### Tested Windows 11, MinGW GCC 15.2, RTX 2080 Ti, Sonic Origins Plus (PPSA05324), closing the window with `WM_CLOSE` on the main menu: - Without this change the process ends with `0xc0000005` in nvoglv64 under `DeviceBuffer::release` <- `DeviceBuffer::~DeviceBuffer` <- `VulkanDevice::~VulkanDevice` <- `Driver::Shutdown`, called from the VideoOut thread. - With it the process exits with code 0 about 3 s after `WM_CLOSE`, without a crash report. Both runs used my working branch (this change on top of other driver work). This branch builds on Windows and the 165 agc driver, recompiler and execution test targets pass. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!849
No description provided.