diff --git a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs index e22e0516c9..5c4e1aea4f 100644 --- a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs +++ b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs @@ -9,7 +9,7 @@ namespace SixLabors.ImageSharp.Diagnostics /// /// Represents the method to handle . /// - public delegate void UndisposedMemoryResourceDelegate(string allocationStackTrace); + public delegate void UndisposedAllocationDelegate(string allocationStackTrace); /// /// Utilities to track memory usage and detect memory leaks from not disposing ImageSharp objects. @@ -18,8 +18,8 @@ namespace SixLabors.ImageSharp.Diagnostics { private static int totalUndisposedAllocationCount; - private static UndisposedMemoryResourceDelegate undisposedMemoryResource; - private static int undisposedMemoryResourceSubscriptionCounter; + private static UndisposedAllocationDelegate undisposedAllocation; + private static int undisposedAllocationSubscriptionCounter; private static readonly object SyncRoot = new(); /// @@ -27,14 +27,14 @@ namespace SixLabors.ImageSharp.Diagnostics /// The event brings significant overhead, and is intended to be used for troubleshooting only. /// For production diagnostics, use . /// - public static event UndisposedMemoryResourceDelegate UndisposedAllocation + public static event UndisposedAllocationDelegate UndisposedAllocation { add { lock (SyncRoot) { - undisposedMemoryResourceSubscriptionCounter++; - undisposedMemoryResource += value; + undisposedAllocationSubscriptionCounter++; + undisposedAllocation += value; } } @@ -42,8 +42,8 @@ namespace SixLabors.ImageSharp.Diagnostics { lock (SyncRoot) { - undisposedMemoryResource -= value; - undisposedMemoryResourceSubscriptionCounter--; + undisposedAllocation -= value; + undisposedAllocationSubscriptionCounter--; } } } @@ -53,7 +53,7 @@ namespace SixLabors.ImageSharp.Diagnostics /// public static int TotalUndisposedAllocationCount => totalUndisposedAllocationCount; - internal static bool MemoryResourceLeakedSubscribed => Volatile.Read(ref undisposedMemoryResourceSubscriptionCounter) > 0; + internal static bool UndisposedAllocationSubscribed => Volatile.Read(ref undisposedAllocationSubscriptionCounter) > 0; internal static void IncrementTotalUndisposedAllocationCount() => Interlocked.Increment(ref totalUndisposedAllocationCount); @@ -63,13 +63,16 @@ namespace SixLabors.ImageSharp.Diagnostics internal static void RaiseUndisposedMemoryResource(string allocationStackTrace) { - if (undisposedMemoryResource is null) + if (undisposedAllocation is null) { return; } // Schedule on the ThreadPool, to avoid user callback messing up the finalizer thread. - ThreadPool.QueueUserWorkItem(_ => undisposedMemoryResource?.Invoke(allocationStackTrace)); + ThreadPool.QueueUserWorkItem( + stackTrace => undisposedAllocation?.Invoke(stackTrace), + allocationStackTrace, + preferLocal: true); } } } diff --git a/src/ImageSharp/Memory/Allocators/Internals/RefCountedMemoryLifetimeGuard.cs b/src/ImageSharp/Memory/Allocators/Internals/RefCountedMemoryLifetimeGuard.cs index 333ef85c25..1c7d6cdfa9 100644 --- a/src/ImageSharp/Memory/Allocators/Internals/RefCountedMemoryLifetimeGuard.cs +++ b/src/ImageSharp/Memory/Allocators/Internals/RefCountedMemoryLifetimeGuard.cs @@ -21,7 +21,7 @@ namespace SixLabors.ImageSharp.Memory.Internals protected RefCountedMemoryLifetimeGuard() { - if (MemoryDiagnostics.MemoryResourceLeakedSubscribed) + if (MemoryDiagnostics.UndisposedAllocationSubscribed) { this.allocationStackTrace = Environment.StackTrace; }