OpenUSD icon indicating copy to clipboard operation
OpenUSD copied to clipboard

Fix resource leak in hdSt

Open dgovil opened this issue 1 year ago • 7 comments

Description of Change(s)

Garbage collect the texture handle registry and resource registry to prevent memory leaks.

Submitting on behalf of Maddy Adams

  • [X] I have verified that all unit tests pass with the proposed changes
  • [X] I have submitted a signed Contributor License Agreement

dgovil avatar May 06 '24 18:05 dgovil

Filed as internal issue #USD-9643

jesschimein avatar May 07 '24 16:05 jesschimein

/AzurePipelines run

jesschimein avatar May 07 '24 16:05 jesschimein

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar May 07 '24 16:05 azure-pipelines[bot]

I am missing some context about how this memory leak came about and what the repro steps are.

Looking at this again, I noticed that HdStResourceRegistry::GarbageCollect doesn't call GarbageCollect on the HdSt_TextureHandleRegistry. That seems a problem.

My suggested fix is this: Rename HdSt_TextureHandleRegistry::_GarbageCollectAndComputeTargetMemory to GarbageCollect and make it public. Call it from HdStResourceRegistry::GarbageCollect. HdStResourceRegistry::~HdStResourceRegistry calls GarbageCollect already which should be called from HdStRenderDelegate::~HdStRenderDelegate when dropping the shared_ptr to the resource registry.

That should fix the memory leak under the following assumptions:

  1. We fully depopulate the render delegate so that no materials or render buffers are holding on to the textures.
  2. No one but the render delegate is holding on to a shared ptr to the resource registry. HdStRenderDelegate::GetResourceRegistry should arguably return a raw pointer.
  3. The Hgi instance is alive when we destruct the HdStRenderDelegate.

unhyperbolic avatar Jul 16 '24 20:07 unhyperbolic

In a couple of days, the change I described above and checked in should appear here. Can you confirm it fixes the leak?

unhyperbolic avatar Jul 17 '24 18:07 unhyperbolic

I’ll be out for a couple weeks but I’ll confirm when I’m back.

On Wed, Jul 17, 2024 at 11:39 AM Matthias Goerner @.***> wrote:

In a couple of days, the change I described above and checked in should appear here. Can you confirm it fixes the leak?

— Reply to this email directly, view it on GitHub https://github.com/PixarAnimationStudios/OpenUSD/pull/3070#issuecomment-2234002308, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABB4XUXORTB7VGDVPF37JFTZM227XAVCNFSM6AAAAABHJPGCUSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMZUGAYDEMZQHA . You are receiving this because you authored the thread.Message ID: @.***>

dgovil avatar Jul 17 '24 18:07 dgovil

@dgovil the commit Matthias is referring to is here: https://github.com/PixarAnimationStudios/OpenUSD/commit/470623ac6484175f79cbc315c44d7568e9d7a6fa ... Let us know if this fixes the leak! If so I think we can close the PR out; otherwise we can iterate.

Thanks for finding this!

tcauchois avatar Jul 26 '24 19:07 tcauchois

In my testing the latest patches seemed to have resolved this. I'll reopen if we encounter it again.

Thank you!

dgovil avatar Sep 05 '24 00:09 dgovil