From a736ed031d09401daa83bab2f5c16ec06071e13e Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Mon, 20 Nov 2023 17:09:22 -0800 Subject: [PATCH] clean up graphics resource code --- src/Graphics/GraphicsDevice.cs | 3 +-- src/Graphics/GraphicsResource.cs | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Graphics/GraphicsDevice.cs b/src/Graphics/GraphicsDevice.cs index c44ded38..82552030 100644 --- a/src/Graphics/GraphicsDevice.cs +++ b/src/Graphics/GraphicsDevice.cs @@ -380,8 +380,7 @@ namespace MoonWorks.Graphics { foreach (var resource in resources) { - var target = resource.Target; - if (target is IDisposable disposable) + if (resource.Target is IDisposable disposable) { disposable.Dispose(); } diff --git a/src/Graphics/GraphicsResource.cs b/src/Graphics/GraphicsResource.cs index dc4a4511..e124e4b2 100644 --- a/src/Graphics/GraphicsResource.cs +++ b/src/Graphics/GraphicsResource.cs @@ -11,18 +11,18 @@ namespace MoonWorks.Graphics public bool IsDisposed { get; private set; } protected abstract Action QueueDestroyFunction { get; } - internal GCHandle selfReference; + private GCHandle SelfReference; - public GraphicsResource(GraphicsDevice device, bool trackResource = true) + protected GraphicsResource(GraphicsDevice device) { Device = device; - selfReference = GCHandle.Alloc(this, GCHandleType.Weak); - Device.AddResourceReference(selfReference); + SelfReference = GCHandle.Alloc(this, GCHandleType.Weak); + Device.AddResourceReference(SelfReference); } - internal GraphicsResourceDisposalHandle CreateDisposalHandle() - { + private GraphicsResourceDisposalHandle CreateDisposalHandle() + { return new GraphicsResourceDisposalHandle { QueueDestroyAction = QueueDestroyFunction, @@ -30,15 +30,15 @@ namespace MoonWorks.Graphics }; } - protected virtual void Dispose(bool disposing) + protected void Dispose(bool disposing) { if (!IsDisposed) { if (Handle != IntPtr.Zero) { QueueDestroyFunction(Device.Handle, Handle); - Device.RemoveResourceReference(selfReference); - selfReference.Free(); + Device.RemoveResourceReference(SelfReference); + SelfReference.Free(); Handle = IntPtr.Zero; } @@ -50,6 +50,8 @@ namespace MoonWorks.Graphics ~GraphicsResource() { #if DEBUG + // If the graphics device associated with this resource was already disposed, we assume + // that your game is in the middle of shutting down. if (!IsDisposed && Device != null && !Device.IsDisposed) { // If you see this log message, you leaked a graphics resource without disposing it!