Skip to content

Commit c955e1f

Browse files
committed
Make sure Streams are disposed of.
1 parent edc1621 commit c955e1f

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/NerdBank.GitVersioning.Tests/ManagedGit/GitPackTests.cs

+18-8
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,24 @@ public void TryGetObjectTest()
136136
using (SHA1 sha = SHA1.Create())
137137
{
138138
Assert.True(gitPack.TryGetObject(GitObjectId.Parse("f5b401f40ad83f13030e946c9ea22cb54cb853cd"), "commit", out Stream commitStream));
139-
140-
// This commit is not deltafied. It is stored as a .gz-compressed stream in the pack file.
141-
var zlibStream = Assert.IsType<ZLibStream>(commitStream);
142-
var deflateStream = Assert.IsType<DeflateStream>(zlibStream.BaseStream);
143-
var pooledStream = Assert.IsType<MemoryMappedStream>(deflateStream.BaseStream);
144-
145-
Assert.Equal(222, commitStream.Length);
146-
Assert.Equal("/zgldANj+jvgOwlecnOKylZDVQg=", Convert.ToBase64String(sha.ComputeHash(commitStream)));
139+
using (commitStream)
140+
{
141+
// This commit is not deltafied. It is stored as a .gz-compressed stream in the pack file.
142+
var zlibStream = Assert.IsType<ZLibStream>(commitStream);
143+
var deflateStream = Assert.IsType<DeflateStream>(zlibStream.BaseStream);
144+
145+
if (IntPtr.Size > 4)
146+
{
147+
var pooledStream = Assert.IsType<MemoryMappedStream>(deflateStream.BaseStream);
148+
}
149+
else
150+
{
151+
var directAccessStream = Assert.IsType<FileStream>(deflateStream.BaseStream);
152+
}
153+
154+
Assert.Equal(222, commitStream.Length);
155+
Assert.Equal("/zgldANj+jvgOwlecnOKylZDVQg=", Convert.ToBase64String(sha.ComputeHash(commitStream)));
156+
}
147157
}
148158
}
149159

src/NerdBank.GitVersioning/ManagedGit/GitPack.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,17 @@ public Stream GetObject(long offset, string objectType)
205205
}
206206

207207
var packStream = this.GetPackStream();
208-
Stream objectStream = GitPackReader.GetObject(this, packStream, offset, objectType, packObjectType);
208+
Stream objectStream;
209+
210+
try
211+
{
212+
objectStream = GitPackReader.GetObject(this, packStream, offset, objectType, packObjectType);
213+
}
214+
catch
215+
{
216+
packStream.Dispose();
217+
throw;
218+
}
209219

210220
return this.cache.Add(offset, objectStream);
211221
}

0 commit comments

Comments
 (0)