Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispose disposables in CommandSearcher and DscResourceSearcher #18206

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/System.Management.Automation/engine/CommandSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1639,8 +1639,24 @@ public void Reset()

_currentMatch = null;
_currentState = SearchState.SearchingAliases;
_matchingAlias = null;
_matchingCmdlet = null;

if (_matchingAlias != null)
{
_matchingAlias.Dispose();
_matchingAlias = null;
}

if (_matchingCmdlet != null)
{
_matchingCmdlet.Dispose();
_matchingCmdlet = null;
}

if (_matchingFunctionEnumerator != null)
{
_matchingFunctionEnumerator.Dispose();
_matchingFunctionEnumerator = null;
}
}

internal CommandOrigin CommandOrigin
Expand Down
27 changes: 24 additions & 3 deletions src/System.Management.Automation/engine/DscResourceSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class DscResourceSearcher : IEnumerable<DscResourceInfo>, IEnumerator<D

private readonly string _resourceName = null;
private readonly ExecutionContext _context = null;
private bool _disposed;
private DscResourceInfo _currentMatch = null;
private IEnumerator<DscResourceInfo> _matchingResource = null;
private Collection<DscResourceInfo> _matchingResourceList = null;
Expand All @@ -43,15 +44,20 @@ internal class DscResourceSearcher : IEnumerable<DscResourceInfo>, IEnumerator<D
public void Reset()
{
_currentMatch = null;
_matchingResource = null;

if (_matchingResource != null)
{
_matchingResource.Dispose();
_matchingResource = null;
}
}

/// <summary>
/// Reset and dispose the Iterator.
/// </summary>
public void Dispose()
{
Reset();
Dispose(true);
GC.SuppressFinalize(this);
}

Expand Down Expand Up @@ -111,6 +117,21 @@ object IEnumerator.Current

#endregion

#region IDisposable

protected virtual void Dispose(bool disposing)
{
if (_disposed || !disposing)
{
return;
}

Reset();
_disposed = true;
}

#endregion

#region private methods

/// <summary>
Expand Down Expand Up @@ -197,7 +218,7 @@ private DscResourceInfo GetNextDscResource()

if (!_matchingResource.MoveNext())
{
_matchingResource = null;
Reset();
}
else
{
Expand Down