Browse Source

consolidate naming

pull/1969/head
Anton Firszov 4 years ago
parent
commit
60108df2c5
  1. 25
      src/ImageSharp/Diagnostics/MemoryDiagnostics.cs
  2. 2
      src/ImageSharp/Memory/Allocators/Internals/RefCountedMemoryLifetimeGuard.cs

25
src/ImageSharp/Diagnostics/MemoryDiagnostics.cs

@ -9,7 +9,7 @@ namespace SixLabors.ImageSharp.Diagnostics
/// <summary> /// <summary>
/// Represents the method to handle <see cref="MemoryDiagnostics.UndisposedAllocation"/>. /// Represents the method to handle <see cref="MemoryDiagnostics.UndisposedAllocation"/>.
/// </summary> /// </summary>
public delegate void UndisposedMemoryResourceDelegate(string allocationStackTrace); public delegate void UndisposedAllocationDelegate(string allocationStackTrace);
/// <summary> /// <summary>
/// Utilities to track memory usage and detect memory leaks from not disposing ImageSharp objects. /// 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 int totalUndisposedAllocationCount;
private static UndisposedMemoryResourceDelegate undisposedMemoryResource; private static UndisposedAllocationDelegate undisposedAllocation;
private static int undisposedMemoryResourceSubscriptionCounter; private static int undisposedAllocationSubscriptionCounter;
private static readonly object SyncRoot = new(); private static readonly object SyncRoot = new();
/// <summary> /// <summary>
@ -27,14 +27,14 @@ namespace SixLabors.ImageSharp.Diagnostics
/// The event brings significant overhead, and is intended to be used for troubleshooting only. /// The event brings significant overhead, and is intended to be used for troubleshooting only.
/// For production diagnostics, use <see cref="TotalUndisposedAllocationCount"/>. /// For production diagnostics, use <see cref="TotalUndisposedAllocationCount"/>.
/// </summary> /// </summary>
public static event UndisposedMemoryResourceDelegate UndisposedAllocation public static event UndisposedAllocationDelegate UndisposedAllocation
{ {
add add
{ {
lock (SyncRoot) lock (SyncRoot)
{ {
undisposedMemoryResourceSubscriptionCounter++; undisposedAllocationSubscriptionCounter++;
undisposedMemoryResource += value; undisposedAllocation += value;
} }
} }
@ -42,8 +42,8 @@ namespace SixLabors.ImageSharp.Diagnostics
{ {
lock (SyncRoot) lock (SyncRoot)
{ {
undisposedMemoryResource -= value; undisposedAllocation -= value;
undisposedMemoryResourceSubscriptionCounter--; undisposedAllocationSubscriptionCounter--;
} }
} }
} }
@ -53,7 +53,7 @@ namespace SixLabors.ImageSharp.Diagnostics
/// </summary> /// </summary>
public static int TotalUndisposedAllocationCount => totalUndisposedAllocationCount; 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() => internal static void IncrementTotalUndisposedAllocationCount() =>
Interlocked.Increment(ref totalUndisposedAllocationCount); Interlocked.Increment(ref totalUndisposedAllocationCount);
@ -63,13 +63,16 @@ namespace SixLabors.ImageSharp.Diagnostics
internal static void RaiseUndisposedMemoryResource(string allocationStackTrace) internal static void RaiseUndisposedMemoryResource(string allocationStackTrace)
{ {
if (undisposedMemoryResource is null) if (undisposedAllocation is null)
{ {
return; return;
} }
// Schedule on the ThreadPool, to avoid user callback messing up the finalizer thread. // 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);
} }
} }
} }

2
src/ImageSharp/Memory/Allocators/Internals/RefCountedMemoryLifetimeGuard.cs

@ -21,7 +21,7 @@ namespace SixLabors.ImageSharp.Memory.Internals
protected RefCountedMemoryLifetimeGuard() protected RefCountedMemoryLifetimeGuard()
{ {
if (MemoryDiagnostics.MemoryResourceLeakedSubscribed) if (MemoryDiagnostics.UndisposedAllocationSubscribed)
{ {
this.allocationStackTrace = Environment.StackTrace; this.allocationStackTrace = Environment.StackTrace;
} }

Loading…
Cancel
Save