Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,26 +206,11 @@ private bool FilterMatch(ManagementObject obj)
#region "IDisposable Members"

/// <summary>
/// Dispose Method.
/// Release all resources.
/// </summary>
public void Dispose()
{
this.Dispose(true);
// Use SuppressFinalize in case a subclass
// of this type implements a finalizer.
GC.SuppressFinalize(this);
}

/// <summary>
/// Dispose Method.
/// </summary>
/// <param name="disposing"></param>
public void Dispose(bool disposing)
{
if (disposing)
{
_searchProcess?.Dispose();
}
_searchProcess?.Dispose();
}

#endregion "IDisposable Members"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2188,15 +2188,12 @@ protected override void BeginProcessing()
#region IDisposable Overrides

/// <summary>
/// Dispose WaitHandle used to honor -Wait parameter.
/// Release all resources.
/// </summary>
/// <remarks>
/// Dispose WaitHandle used to honor -Wait parameter.
/// </remarks>
public void Dispose()
{
Dispose(true);
System.GC.SuppressFinalize(this);
}

private void Dispose(bool isDisposing)
{
_cancellationTokenSource.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,22 @@ protected override void EndProcessing()
_commandWrapper.ShutDown();
}

private void Dispose(bool isDisposing)
/// <summary>
/// Release all resources.
/// </summary>
public void Dispose()
{
if (!_alreadyDisposed)
{
_alreadyDisposed = true;
if (isDisposing && _commandWrapper != null)
if (_commandWrapper != null)
{
_commandWrapper.Dispose();
_commandWrapper = null;
}
}
}

/// <summary>
/// Dispose method in IDisposable.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

#region private
private CommandWrapper _commandWrapper;
private bool _alreadyDisposed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,12 @@ public string[] LiteralPath
private bool _disposed = false;

/// <summary>
/// Public dispose method.
/// Release all resources.
/// </summary>
public void Dispose()
{
if (!_disposed)
{
GC.SuppressFinalize(this);
if (_helper != null)
{
_helper.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,29 +297,17 @@ protected override void StopProcessing()
#region IDisposable Members

/// <summary>
/// Releases resources associated with this object.
/// Release all resources.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Releases resources associated with this object.
/// </summary>
private void Dispose(bool disposing)
{
if (_disposed)
{
return;
}

if (disposing)
{
_cancellationTokenSource.Dispose();
}

_cancellationTokenSource.Dispose();

_disposed = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1780,28 +1780,16 @@ protected override void StopProcessing()
#region IDisposable Members

/// <summary>
/// Releases resources associated with this object.
/// Release all resources.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Releases resources associated with this object.
/// </summary>
private void Dispose(bool disposing)
{
if (_disposed)
{
return;
}

if (disposing)
{
_cancellationTokenSource.Dispose();
}
_cancellationTokenSource.Dispose();

_disposed = true;
}
Expand Down
11 changes: 5 additions & 6 deletions src/System.Management.Automation/engine/MshCommandRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2372,19 +2372,18 @@ internal AllowWrite(InternalCommand permittedToWrite, bool permittedToWriteToPip
_pp._permittedToWriteToPipeline = permittedToWriteToPipeline;
_pp._permittedToWriteThread = Thread.CurrentThread;
}

/// <summary>
/// End the scope where WriteObject/WriteError is permitted.
/// Release all resources.
/// </summary>
/// <!--
/// Not a true public, since the class is internal.
/// This is public only due to C# interface rules.
/// -->
/// <remarks>
/// End the scope where WriteObject/WriteError is permitted.
/// </remarks>
public void Dispose()
{
_pp._permittedToWrite = _wasPermittedToWrite;
_pp._permittedToWriteToPipeline = _wasPermittedToWriteToPipeline;
_pp._permittedToWriteThread = _wasPermittedToWriteThread;
GC.SuppressFinalize(this);
}

// There is no finalizer, by design. This class relies on always
Expand Down
100 changes: 41 additions & 59 deletions src/System.Management.Automation/engine/hostifaces/PowerShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3880,15 +3880,50 @@ private void PipelineStateChanged(object source, PipelineStateEventArgs stateEve
#region IDisposable Overrides

/// <summary>
/// Dispose all managed resources. This will suppress finalizer on the object from getting called by
/// calling System.GC.SuppressFinalize(this).
/// Release all resources.
/// </summary>
public void Dispose()
{
Dispose(true);
// To prevent derived types with finalizers from having to re-implement System.IDisposable to call it,
// unsealed types without finalizers should still call SuppressFinalize.
System.GC.SuppressFinalize(this);
lock (_syncObject)
{
// if already disposed return
if (_isDisposed)
{
return;
}
}

// Stop the currently running command outside of the lock
if (InvocationStateInfo.State == PSInvocationState.Running ||
InvocationStateInfo.State == PSInvocationState.Stopping)
{
Stop();
}

lock (_syncObject)
{
_isDisposed = true;
}

if (OutputBuffer != null && OutputBufferOwner)
{
OutputBuffer.Dispose();
}

if (_errorBuffer != null && ErrorBufferOwner)
{
_errorBuffer.Dispose();
}

if (IsRunspaceOwner)
{
_runspace.Dispose();
}

RemotePowerShell?.Dispose();

_invokeAsyncResult = null;
_stopAsyncResult = null;
}

#endregion
Expand Down Expand Up @@ -4121,59 +4156,6 @@ private void AssertNotDisposed()
}
}

/// <summary>
/// Release all the resources.
/// </summary>
/// <param name="disposing">
/// if true, release all the managed objects.
/// </param>
private void Dispose(bool disposing)
{
if (disposing)
{
lock (_syncObject)
{
// if already disposed return
if (_isDisposed)
{
return;
}
}

// Stop the currently running command outside of the lock
if (InvocationStateInfo.State == PSInvocationState.Running ||
InvocationStateInfo.State == PSInvocationState.Stopping)
{
Stop();
}

lock (_syncObject)
{
_isDisposed = true;
}

if (OutputBuffer != null && OutputBufferOwner)
{
OutputBuffer.Dispose();
}

if (_errorBuffer != null && ErrorBufferOwner)
{
_errorBuffer.Dispose();
}

if (IsRunspaceOwner)
{
_runspace.Dispose();
}

RemotePowerShell?.Dispose();

_invokeAsyncResult = null;
_stopAsyncResult = null;
}
}

/// <summary>
/// Clear the internal elements.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,9 @@ public bool HasExited
#region Dispose

/// <summary>
/// Implementing the <see cref="IDisposable"/> interface.
/// Release all resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
if (_isDisposed)
{
Expand All @@ -203,23 +197,20 @@ private void Dispose(bool disposing)

_isDisposed = true;
}

if (disposing)

try
{
if (Process != null && !Process.HasExited)
Process.Kill();
}
catch (InvalidOperationException)
{
}
catch (Win32Exception)
{
}
catch (NotSupportedException)
{
try
{
if (Process != null && !Process.HasExited)
Process.Kill();
}
catch (InvalidOperationException)
{
}
catch (Win32Exception)
{
}
catch (NotSupportedException)
{
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ public void EndClose(IAsyncResult asyncResult)
}

/// <summary>
/// Dispose the current runspacepool.
/// Release all resources.
/// </summary>
public void Dispose()
{
Expand Down
15 changes: 3 additions & 12 deletions src/System.Management.Automation/utils/CryptoUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -576,21 +576,12 @@ internal static PSRSACryptoServiceProvider GetRSACryptoServiceProviderForServer(
#region IDisposable

/// <summary>
/// Dispose resources.
/// Release all resources.
/// </summary>
public void Dispose()
{
Dispose(true);
System.GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
if (disposing)
{
_rsa?.Dispose();
_aes?.Dispose();
}
_rsa?.Dispose();
_aes?.Dispose();
}

#endregion IDisposable
Expand Down
Loading