Skip to content

Commit

Permalink
Apply disposal pattern to the new dispose methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Dec 7, 2020
1 parent f4fb3dd commit 75d370f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/NerdBank.GitVersioning/GitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ private protected static void FindGitPaths(string path, out string gitDirectory,
}
}

/// <inheritdoc />
/// <summary>
/// Disposes of native and managed resources associated by this object.
/// </summary>
/// <param name="disposing"><see langword="true" /> to dispose managed and native resources; <see langword="false" /> to only dispose of native resources.</param>
protected virtual void Dispose(bool disposing)
{
}
Expand Down
7 changes: 6 additions & 1 deletion src/NerdBank.GitVersioning/Managed/ManagedGitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ public override string GetShortUniqueCommitId(int minLength)
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
this.Repository.Dispose();
if (disposing)
{
this.Repository.Dispose();
}

base.Dispose(disposing);
}

/// <summary>
Expand Down
12 changes: 11 additions & 1 deletion src/NerdBank.GitVersioning/ManagedGit/GitPackCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ public abstract class GitPackCache : IDisposable
public abstract Stream Add(long offset, Stream stream);

/// <inheritdoc/>
public virtual void Dispose()
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Disposes of native and managed resources associated by this object.
/// </summary>
/// <param name="disposing"><see langword="true" /> to dispose managed and native resources; <see langword="false" /> to only dispose of native resources.</param>
protected virtual void Dispose(bool disposing)
{
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/NerdBank.GitVersioning/ManagedGit/GitPackMemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,20 @@ public override void GetCacheStatistics(StringBuilder builder)
}

/// <inheritdoc/>
public override void Dispose()
protected override void Dispose(bool disposing)
{
while (this.cache.Count > 0)
if (disposing)
{
var key = this.cache.Keys.First();
var stream = this.cache[key];
stream.Dispose();
this.cache.Remove(key);
while (this.cache.Count > 0)
{
var key = this.cache.Keys.First();
var stream = this.cache[key];
stream.Dispose();
this.cache.Remove(key);
}
}

base.Dispose(disposing);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,13 @@ public override void Write(byte[] buffer, int offset, int count)

protected override void Dispose(bool disposing)
{
this.stream.Dispose();
this.cacheStream.Dispose();
if (disposing)
{
this.stream.Dispose();
this.cacheStream.Dispose();
}

base.Dispose(disposing);
}

private void DisposeStreamIfRead()
Expand Down
9 changes: 7 additions & 2 deletions src/NerdBank.GitVersioning/ManagedGit/MemoryMappedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,13 @@ public override void Write(byte[] buffer, int offset, int count)
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
this.accessor.SafeMemoryMappedViewHandle.ReleasePointer();
this.disposed = true;
if (disposing)
{
this.accessor.SafeMemoryMappedViewHandle.ReleasePointer();
this.disposed = true;
}

base.Dispose(disposing);
}
}
}

0 comments on commit 75d370f

Please sign in to comment.