From cdf83ad2fc5653072b9eecbcd8d6944c4bb73008 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 1 Sep 2022 23:54:54 +0900 Subject: [PATCH] internal/atlas: dispose images at ReadPixels Without resolveDeferred() at ReadPixels, many images are never disposed in tests. Updates #2292 --- internal/atlas/image.go | 4 ++++ internal/graphicsdriver/directx/graphics_windows.go | 1 + 2 files changed, 5 insertions(+) diff --git a/internal/atlas/image.go b/internal/atlas/image.go index e0f82f37f..e5edee845 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -576,6 +576,10 @@ func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte backendsM.Lock() defer backendsM.Unlock() + // In the tests, BeginFrame might not be called often and then images might not be disposed (#2292). + // To prevent memory leaks, resolve the deferred functions here. + resolveDeferred() + if i.backend == nil || i.backend.restorable == nil { for i := range pixels { pixels[i] = 0 diff --git a/internal/graphicsdriver/directx/graphics_windows.go b/internal/graphicsdriver/directx/graphics_windows.go index 9449eadb6..119ec0c27 100644 --- a/internal/graphicsdriver/directx/graphics_windows.go +++ b/internal/graphicsdriver/directx/graphics_windows.go @@ -778,6 +778,7 @@ func (g *Graphics) End(present bool) error { if err := g.waitForCommandQueue(); err != nil { return err } + g.releaseResources(g.frameIndex) g.releaseVerticesAndIndices(g.frameIndex) }