Skip to content

Commit c7be068

Browse files
committed
Mark Repository.Configuration for cleanup
1 parent 65aa0aa commit c7be068

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

LibGit2Sharp/Core/Handles/SafeHandleExtensions.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1-
namespace LibGit2Sharp.Core.Handles
1+
using System;
2+
3+
namespace LibGit2Sharp.Core.Handles
24
{
35
internal static class SafeHandleExtensions
46
{
7+
public static void SafeDispose(this IDisposable disposable)
8+
{
9+
if (disposable == null)
10+
return;
11+
12+
var handle = disposable as SafeHandleBase;
13+
if (handle != null)
14+
{
15+
SafeDispose(handle);
16+
return;
17+
}
18+
19+
disposable.Dispose();
20+
}
21+
522
public static void SafeDispose(this SafeHandleBase handle)
623
{
724
if (handle == null || handle.IsClosed || handle.IsInvalid)

LibGit2Sharp/Repository.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class Repository : IDisposable
2626
private readonly Diff diff;
2727
private readonly NoteCollection notes;
2828
private readonly Lazy<ObjectDatabase> odb;
29-
private readonly Stack<SafeHandleBase> handlesToCleanup = new Stack<SafeHandleBase>();
29+
private readonly Stack<IDisposable> toCleanup = new Stack<IDisposable>();
3030
private static readonly Lazy<string> versionRetriever = new Lazy<string>(RetrieveVersion);
3131

3232
/// <summary>
@@ -89,7 +89,7 @@ public Repository(string path, RepositoryOptions options = null)
8989
branches = new BranchCollection(this);
9090
tags = new TagCollection(this);
9191
info = new Lazy<RepositoryInformation>(() => new RepositoryInformation(this, isBare));
92-
config = new Lazy<Configuration>(() => new Configuration(this));
92+
config = new Lazy<Configuration>(() => RegisterForCleanup(new Configuration(this)));
9393
remotes = new Lazy<RemoteCollection>(() => new RemoteCollection(this));
9494
odb = new Lazy<ObjectDatabase>(() => new ObjectDatabase(this));
9595
diff = new Diff(this);
@@ -244,9 +244,9 @@ public void Dispose()
244244
/// </summary>
245245
protected virtual void Dispose(bool disposing)
246246
{
247-
while (handlesToCleanup.Count > 0)
247+
while (toCleanup.Count > 0)
248248
{
249-
handlesToCleanup.Pop().SafeDispose();
249+
toCleanup.Pop().SafeDispose();
250250
}
251251
}
252252

@@ -481,9 +481,10 @@ public void Reset(ResetOptions resetOptions, string shaOrReferenceName)
481481
throw new NotImplementedException();
482482
}
483483

484-
internal void RegisterForCleanup(SafeHandleBase handleToCleanup)
484+
internal T RegisterForCleanup<T>(T disposable) where T : IDisposable
485485
{
486-
handlesToCleanup.Push(handleToCleanup);
486+
toCleanup.Push(disposable);
487+
return disposable;
487488
}
488489

489490
/// <summary>

0 commit comments

Comments
 (0)