diff options
author | nulltoken <emeric.fermas@gmail.com> | 2012-02-28 20:45:29 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2012-02-28 22:46:52 +0400 |
commit | 8da9472dc250d879d8cfae029cd8ee395edc9469 (patch) | |
tree | 52611eef5d8e82bda1069092babc53ff9acb528e /LibGit2Sharp | |
parent | 8e14827a821178e705b629e48acfdf8e6abe0e23 (diff) |
Refactor the release of some native handles
This also fixes a RevWalk related memory leak which was preventing some temporary test files from being deleted.
Diffstat (limited to 'LibGit2Sharp')
-rw-r--r-- | LibGit2Sharp/CommitCollection.cs | 2 | ||||
-rw-r--r-- | LibGit2Sharp/Index.cs | 25 | ||||
-rw-r--r-- | LibGit2Sharp/Repository.cs | 16 |
3 files changed, 15 insertions, 28 deletions
diff --git a/LibGit2Sharp/CommitCollection.cs b/LibGit2Sharp/CommitCollection.cs index 27f62276..1bcc43e7 100644 --- a/LibGit2Sharp/CommitCollection.cs +++ b/LibGit2Sharp/CommitCollection.cs @@ -190,6 +190,8 @@ namespace LibGit2Sharp { this.repo = repo; int res = NativeMethods.git_revwalk_new(out handle, repo.Handle); + repo.RegisterForCleanup(handle); + Ensure.Success(res); Sort(sortingStrategy); diff --git a/LibGit2Sharp/Index.cs b/LibGit2Sharp/Index.cs index 0241312c..d4285673 100644 --- a/LibGit2Sharp/Index.cs +++ b/LibGit2Sharp/Index.cs @@ -12,7 +12,7 @@ namespace LibGit2Sharp /// The Index is a staging area between the Working directory and the Repository. /// It's used to prepare and aggregate the changes that will be part of the next commit. /// </summary> - public class Index : IEnumerable<IndexEntry>, IDisposable + public class Index : IEnumerable<IndexEntry> { private readonly IndexSafeHandle handle; private readonly Repository repo; @@ -24,6 +24,8 @@ namespace LibGit2Sharp this.repo = repo; int res = NativeMethods.git_repository_index(out handle, repo.Handle); Ensure.Success(res); + + repo.RegisterForCleanup(handle); } internal IndexSafeHandle Handle @@ -72,27 +74,6 @@ namespace LibGit2Sharp } } - #region IDisposable Members - - /// <summary> - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// </summary> - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - /// <summary> - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// </summary> - protected virtual void Dispose(bool disposing) - { - handle.SafeDispose(); - } - - #endregion - #region IEnumerable<IndexEntry> Members /// <summary> diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index e0e01342..c18d2676 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using LibGit2Sharp.Core; using LibGit2Sharp.Core.Compat; @@ -19,6 +20,7 @@ namespace LibGit2Sharp private readonly TagCollection tags; private readonly Lazy<RepositoryInformation> info; private readonly bool isBare; + private readonly List<SafeHandleBase> handlesToCleanup = new List<SafeHandleBase>(); /// <summary> /// Initializes a new instance of the <see cref = "Repository" /> class. @@ -35,6 +37,8 @@ namespace LibGit2Sharp int res = NativeMethods.git_repository_open(out handle, PosixPathHelper.ToPosix(path)); Ensure.Success(res); + RegisterForCleanup(handle); + isBare = NativeMethods.RepositoryStateChecker(handle, NativeMethods.git_repository_is_bare); if (!isBare) @@ -156,12 +160,7 @@ namespace LibGit2Sharp /// </summary> protected virtual void Dispose(bool disposing) { - handle.SafeDispose(); - - if (index != null) - { - index.Dispose(); - } + handlesToCleanup.ForEach(handleToCleanup => handleToCleanup.SafeDispose()); } #endregion @@ -334,5 +333,10 @@ namespace LibGit2Sharp throw new NotImplementedException(); } + + internal void RegisterForCleanup(SafeHandleBase handleToCleanup) + { + handlesToCleanup.Add(handleToCleanup); + } } } |