From 14ce85627de373df72c262ead31206c3a2abacfa Mon Sep 17 00:00:00 2001 From: goat Date: Sun, 26 Mar 2023 00:22:09 +0100 Subject: [PATCH] fix: correctly enqueue textures for destroy if they're GC'd --- .../Interface/Internal/DalamudTextureWrap.cs | 19 ++++++++++++++++++- .../Interface/Internal/InterfaceManager.cs | 12 ++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Dalamud/Interface/Internal/DalamudTextureWrap.cs b/Dalamud/Interface/Internal/DalamudTextureWrap.cs index 346b7cd24..97fb1dd0b 100644 --- a/Dalamud/Interface/Internal/DalamudTextureWrap.cs +++ b/Dalamud/Interface/Internal/DalamudTextureWrap.cs @@ -21,6 +21,14 @@ public class DalamudTextureWrap : TextureWrap this.wrappedWrap = wrappingWrap; } + /// + /// Finalizes an instance of the class. + /// + ~DalamudTextureWrap() + { + this.Dispose(false); + } + /// /// Gets the ImGui handle of the texture. /// @@ -41,7 +49,8 @@ public class DalamudTextureWrap : TextureWrap /// public void Dispose() { - Service.Get().EnqueueDeferredDispose(this); + this.Dispose(true); + GC.SuppressFinalize(this); } /// @@ -51,4 +60,12 @@ public class DalamudTextureWrap : TextureWrap { this.wrappedWrap.Dispose(); } + + private void Dispose(bool disposing) + { + if (disposing) + { + Service.GetNullable()?.EnqueueDeferredDispose(this); + } + } } diff --git a/Dalamud/Interface/Internal/InterfaceManager.cs b/Dalamud/Interface/Internal/InterfaceManager.cs index aa721ad39..9a8da773c 100644 --- a/Dalamud/Interface/Internal/InterfaceManager.cs +++ b/Dalamud/Interface/Internal/InterfaceManager.cs @@ -606,12 +606,16 @@ internal class InterfaceManager : IDisposable, IServiceType this.RenderImGui(); - foreach (var texture in this.deferredDisposeTextures) + if (this.deferredDisposeTextures.Count > 0) { - texture.RealDispose(); - } + Log.Verbose("[IM] Disposing {Count} textures", this.deferredDisposeTextures.Count); + foreach (var texture in this.deferredDisposeTextures) + { + texture.RealDispose(); + } - this.deferredDisposeTextures.Clear(); + this.deferredDisposeTextures.Clear(); + } return this.presentHook.Original(swapChain, syncInterval, presentFlags); }