Skip to content

Commit 75d370f

Browse files
committed
Apply disposal pattern to the new dispose methods
1 parent f4fb3dd commit 75d370f

File tree

6 files changed

+46
-13
lines changed

6 files changed

+46
-13
lines changed

src/NerdBank.GitVersioning/GitContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,10 @@ private protected static void FindGitPaths(string path, out string gitDirectory,
246246
}
247247
}
248248

249-
/// <inheritdoc />
249+
/// <summary>
250+
/// Disposes of native and managed resources associated by this object.
251+
/// </summary>
252+
/// <param name="disposing"><see langword="true" /> to dispose managed and native resources; <see langword="false" /> to only dispose of native resources.</param>
250253
protected virtual void Dispose(bool disposing)
251254
{
252255
}

src/NerdBank.GitVersioning/Managed/ManagedGitContext.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,12 @@ public override string GetShortUniqueCommitId(int minLength)
122122
/// <inheritdoc/>
123123
protected override void Dispose(bool disposing)
124124
{
125-
this.Repository.Dispose();
125+
if (disposing)
126+
{
127+
this.Repository.Dispose();
128+
}
129+
130+
base.Dispose(disposing);
126131
}
127132

128133
/// <summary>

src/NerdBank.GitVersioning/ManagedGit/GitPackCache.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,17 @@ public abstract class GitPackCache : IDisposable
5454
public abstract Stream Add(long offset, Stream stream);
5555

5656
/// <inheritdoc/>
57-
public virtual void Dispose()
57+
public void Dispose()
58+
{
59+
this.Dispose(true);
60+
GC.SuppressFinalize(this);
61+
}
62+
63+
/// <summary>
64+
/// Disposes of native and managed resources associated by this object.
65+
/// </summary>
66+
/// <param name="disposing"><see langword="true" /> to dispose managed and native resources; <see langword="false" /> to only dispose of native resources.</param>
67+
protected virtual void Dispose(bool disposing)
5868
{
5969
}
6070
}

src/NerdBank.GitVersioning/ManagedGit/GitPackMemoryCache.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,20 @@ public override void GetCacheStatistics(StringBuilder builder)
5858
}
5959

6060
/// <inheritdoc/>
61-
public override void Dispose()
61+
protected override void Dispose(bool disposing)
6262
{
63-
while (this.cache.Count > 0)
63+
if (disposing)
6464
{
65-
var key = this.cache.Keys.First();
66-
var stream = this.cache[key];
67-
stream.Dispose();
68-
this.cache.Remove(key);
65+
while (this.cache.Count > 0)
66+
{
67+
var key = this.cache.Keys.First();
68+
var stream = this.cache[key];
69+
stream.Dispose();
70+
this.cache.Remove(key);
71+
}
6972
}
73+
74+
base.Dispose(disposing);
7075
}
7176
}
7277
}

src/NerdBank.GitVersioning/ManagedGit/GitPackMemoryCacheStream.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,13 @@ public override void Write(byte[] buffer, int offset, int count)
101101

102102
protected override void Dispose(bool disposing)
103103
{
104-
this.stream.Dispose();
105-
this.cacheStream.Dispose();
104+
if (disposing)
105+
{
106+
this.stream.Dispose();
107+
this.cacheStream.Dispose();
108+
}
109+
110+
base.Dispose(disposing);
106111
}
107112

108113
private void DisposeStreamIfRead()

src/NerdBank.GitVersioning/ManagedGit/MemoryMappedStream.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,13 @@ public override void Write(byte[] buffer, int offset, int count)
144144
/// <inheritdoc/>
145145
protected override void Dispose(bool disposing)
146146
{
147-
this.accessor.SafeMemoryMappedViewHandle.ReleasePointer();
148-
this.disposed = true;
147+
if (disposing)
148+
{
149+
this.accessor.SafeMemoryMappedViewHandle.ReleasePointer();
150+
this.disposed = true;
151+
}
152+
153+
base.Dispose(disposing);
149154
}
150155
}
151156
}

0 commit comments

Comments
 (0)