From 535be6fc7a3909941c6f3e9d233762c05a9efc9a Mon Sep 17 00:00:00 2001 From: Michael Mendy Date: Sat, 1 Jun 2024 15:27:43 -0700 Subject: [PATCH] Added a check in the `Dispose(bool disposing)` method to return early if the object has already been disposed. * Removed unused using directives. * Renamed `disposedValue` to `isDisposed` for better readability. * Added a check in the `Dispose(bool disposing)` method to return early if the object has already been disposed. * Called `GC.SuppressFinalize(this)` in the `Dispose()` method to prevent the finalizer from being called after the object has been disposed. --- P4VS/SwarmAPI/SSLCertificateHandler.cs | 32 ++++++++++++-------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/P4VS/SwarmAPI/SSLCertificateHandler.cs b/P4VS/SwarmAPI/SSLCertificateHandler.cs index 9ca63de..a5fc4a8 100644 --- a/P4VS/SwarmAPI/SSLCertificateHandler.cs +++ b/P4VS/SwarmAPI/SSLCertificateHandler.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; @@ -19,8 +16,7 @@ public void Init(string serverUrl) { ServerUrl = serverUrl; // Override automatic validation of SSL server certificates. - ServicePointManager.ServerCertificateValidationCallback = - ValidateServerCertficate; + ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertficate; } /// @@ -43,21 +39,24 @@ public abstract bool ValidateServerCertficate( SslPolicyErrors sslPolicyErrors); #region IDisposable Support - private bool disposedValue = false; // To detect redundant calls + private bool isDisposed = false; protected virtual void Dispose(bool disposing) { - if (!disposedValue) + if (isDisposed) { - if (disposing) - { - ServicePointManager.ServerCertificateValidationCallback = null; - } - // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. - // TODO: set large fields to null. + return; + } - disposedValue = true; + if (disposing) + { + ServicePointManager.ServerCertificateValidationCallback = null; } + + // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. + // TODO: set large fields to null. + + isDisposed = true; } // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources. @@ -71,8 +70,7 @@ public void Dispose() { // Do not change this code. Put cleanup code in Dispose(bool disposing) above. Dispose(true); - // TODO: uncomment the following line if the finalizer is overridden above. - // GC.SuppressFinalize(this); + GC.SuppressFinalize(this); } #endregion }