From b2a0ca3515d4c27f97197757010785a5815e7fe4 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Mon, 20 Nov 2023 15:18:06 -0800 Subject: [PATCH] replace WeakReference with weak GCHandle --- src/Graphics/GraphicsDevice.cs | 14 ++++++++------ src/Graphics/GraphicsResource.cs | 12 ++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Graphics/GraphicsDevice.cs b/src/Graphics/GraphicsDevice.cs index 3d3a7e13..c44ded38 100644 --- a/src/Graphics/GraphicsDevice.cs +++ b/src/Graphics/GraphicsDevice.cs @@ -2,6 +2,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; +using System.Runtime.InteropServices; using RefreshCS; namespace MoonWorks.Graphics @@ -22,7 +23,7 @@ namespace MoonWorks.Graphics public bool IsDisposed { get; private set; } - private readonly HashSet> resources = new HashSet>(); + private readonly HashSet resources = new HashSet(); private FencePool FencePool; internal GraphicsDevice( @@ -338,7 +339,7 @@ namespace MoonWorks.Graphics return (TextureFormat) Refresh.Refresh_GetSwapchainFormat(Handle, window.Handle); } - internal void AddResourceReference(WeakReference resourceReference) + internal void AddResourceReference(GCHandle resourceReference) { lock (resources) { @@ -346,7 +347,7 @@ namespace MoonWorks.Graphics } } - internal void RemoveResourceReference(WeakReference resourceReference) + internal void RemoveResourceReference(GCHandle resourceReference) { lock (resources) { @@ -377,11 +378,12 @@ namespace MoonWorks.Graphics { lock (resources) { - foreach (var weakReference in resources) + foreach (var resource in resources) { - if (weakReference.TryGetTarget(out var target)) + var target = resource.Target; + if (target is IDisposable disposable) { - target.Dispose(); + disposable.Dispose(); } } resources.Clear(); diff --git a/src/Graphics/GraphicsResource.cs b/src/Graphics/GraphicsResource.cs index c610df26..dc4a4511 100644 --- a/src/Graphics/GraphicsResource.cs +++ b/src/Graphics/GraphicsResource.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.InteropServices; namespace MoonWorks.Graphics { @@ -10,14 +11,14 @@ namespace MoonWorks.Graphics public bool IsDisposed { get; private set; } protected abstract Action QueueDestroyFunction { get; } - internal WeakReference weakReference; + internal GCHandle selfReference; public GraphicsResource(GraphicsDevice device, bool trackResource = true) { Device = device; - weakReference = new WeakReference(this); - Device.AddResourceReference(weakReference); + selfReference = GCHandle.Alloc(this, GCHandleType.Weak); + Device.AddResourceReference(selfReference); } internal GraphicsResourceDisposalHandle CreateDisposalHandle() @@ -36,10 +37,9 @@ namespace MoonWorks.Graphics if (Handle != IntPtr.Zero) { QueueDestroyFunction(Device.Handle, Handle); - Device.RemoveResourceReference(weakReference); - weakReference.SetTarget(null); + Device.RemoveResourceReference(selfReference); + selfReference.Free(); - weakReference = null; Handle = IntPtr.Zero; }