Skip to content

Commit 3664525

Browse files
committed
Prevent uninitialised performance counters escaping CreatePerformanceCounter
1 parent 2655083 commit 3664525

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Diagnostics/SqlClientMetrics.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,25 +493,25 @@ private void RemovePerformanceCounters()
493493

494494
private PerformanceCounter? CreatePerformanceCounter(string counterName, PerformanceCounterType counterType)
495495
{
496-
PerformanceCounter? instance = null;
497-
498496
_instanceName ??= GetInstanceName();
499497
try
500498
{
501-
instance = new PerformanceCounter();
499+
PerformanceCounter instance = new();
502500
instance.CategoryName = PerformanceCounterCategoryName;
503501
instance.CounterName = counterName;
504502
instance.InstanceName = _instanceName;
505503
instance.InstanceLifetime = PerformanceCounterInstanceLifetime.Process;
506504
instance.ReadOnly = false;
507505
instance.RawValue = 0; // make sure we start out at zero
506+
507+
return instance;
508508
}
509509
catch (InvalidOperationException e)
510510
{
511511
ADP.TraceExceptionWithoutRethrow(e);
512-
}
513512

514-
return instance;
513+
return null;
514+
}
515515
}
516516

517517
// SxS: this method uses GetCurrentProcessId to construct the instance name.

0 commit comments

Comments
 (0)