Skip to content

Commit

Permalink
Tweak SafeHandle finalization logging to include a count (dotnet#72223)
Browse files Browse the repository at this point in the history
Helps to understand the impact a certain change makes and also to more easily refer to a given log entry.
  • Loading branch information
stephentoub authored Jul 15, 2022
1 parent 1ac930f commit a379a42
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ namespace System.Runtime.InteropServices
public abstract partial class SafeHandle : CriticalFinalizerObject, IDisposable
{
#if DEBUG
/// <summary>Indicates whether debug tracking and logging of SafeHandle finalization is enabled</summary>
private static readonly bool s_logFinalization = Environment.GetEnvironmentVariable("DEBUG_SAFEHANDLE_FINALIZATION") == "1";
/// <summary>Debug counter for the number of SafeHandles that have been finalized.</summary>
private static long s_safeHandlesFinalized;
#endif

// IMPORTANT:
Expand Down Expand Up @@ -113,7 +116,8 @@ protected virtual void Dispose(bool disposing)
#if DEBUG
if (!disposing && _ctorStackTrace is not null)
{
Internal.Console.WriteLine($"{Environment.NewLine}*** {GetType()} (0x{handle.ToInt64():x}) finalized! Ctor stack:{Environment.NewLine}{_ctorStackTrace}{Environment.NewLine}");
long count = Interlocked.Increment(ref s_safeHandlesFinalized);
Internal.Console.WriteLine($"{Environment.NewLine}*** #{count} {GetType()} (0x{handle.ToInt64():x}) finalized! Ctor stack:{Environment.NewLine}{_ctorStackTrace}{Environment.NewLine}");
}
#endif
Debug.Assert(_fullyInitialized);
Expand Down

0 comments on commit a379a42

Please sign in to comment.