Skip to content

Commit

Permalink
Use SafeHandles (#2127)
Browse files Browse the repository at this point in the history
* Remove unneeded cast

* Use SafeHandles
  • Loading branch information
bording authored Nov 21, 2024
1 parent 66f6940 commit a88e2a0
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 173 deletions.
87 changes: 23 additions & 64 deletions LibGit2Sharp/Core/Handles/Libgit2Object.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
// This activates a lightweight mode which will help put under the light
// incorrectly released handles by outputing a warning message in the console.
// incorrectly released handles by outputting a warning message in the console.
//
// This should be activated when tests are being run on the CI server.
//
// Uncomment the line below or add a conditional symbol to activate this mode

// #define LEAKS_IDENTIFYING
//#define LEAKS_IDENTIFYING

// This activates a more throrough mode which will show the stack trace of the
// This activates a more thorough mode which will show the stack trace of the
// allocation code path for each handle that has been improperly released.
//
// This should be manually activated when some warnings have been raised as
// a result of LEAKS_IDENTIFYING mode activation.
//
// Uncomment the line below or add a conditional symbol to activate this mode

// #define LEAKS_TRACKING
//#define LEAKS_TRACKING

using System;
using System.Linq;
using System.Diagnostics;
using System.Globalization;
using System.Collections.Generic;
using Microsoft.Win32.SafeHandles;

#if LEAKS_IDENTIFYING
namespace LibGit2Sharp.Core
{
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// Holds leaked handle type names reported by <see cref="Core.Handles.Libgit2Object"/>
/// </summary>
Expand Down Expand Up @@ -78,30 +78,27 @@ public static IEnumerable<string> TypeNames

namespace LibGit2Sharp.Core.Handles
{
internal unsafe abstract class Libgit2Object : IDisposable
#if LEAKS_TRACKING
using System.Diagnostics;
using System.Globalization;
#endif

internal unsafe abstract class Libgit2Object : SafeHandleZeroOrMinusOneIsInvalid
{
#if LEAKS_TRACKING
private readonly string trace;
private readonly Guid id;
#endif

protected void* ptr;

internal void* Handle
internal unsafe Libgit2Object(void* ptr, bool owned)
: this(new IntPtr(ptr), owned)
{
get
{
return ptr;
}
}

bool owned;
bool disposed;

internal unsafe Libgit2Object(void* handle, bool owned)
internal unsafe Libgit2Object(IntPtr ptr, bool owned)
: base(owned)
{
this.ptr = handle;
this.owned = owned;
SetHandle(ptr);

#if LEAKS_TRACKING
id = Guid.NewGuid();
Expand All @@ -111,53 +108,20 @@ internal unsafe Libgit2Object(void* handle, bool owned)
#endif
}

internal unsafe Libgit2Object(IntPtr ptr, bool owned)
: this(ptr.ToPointer(), owned)
{
}
internal IntPtr AsIntPtr() => DangerousGetHandle();

~Libgit2Object()
{
Dispose(false);
}

internal bool IsNull
{
get
{
return ptr == null;
}
}

internal IntPtr AsIntPtr()
{
return new IntPtr(ptr);
}

public abstract void Free();

void Dispose(bool disposing)
protected override void Dispose(bool disposing)
{
#if LEAKS_IDENTIFYING
bool leaked = !disposing && ptr != null;
bool leaked = !disposing && DangerousGetHandle() != IntPtr.Zero;

if (leaked)
{
LeaksContainer.Add(GetType().Name);
}
#endif

if (!disposed)
{
if (owned)
{
Free();
}

ptr = null;
}

disposed = true;
base.Dispose(disposing);

#if LEAKS_TRACKING
if (!leaked)
Expand All @@ -172,11 +136,6 @@ void Dispose(bool disposing)
}
#endif
}

public void Dispose()
{
Dispose(true);
}
}
}

Loading

0 comments on commit a88e2a0

Please sign in to comment.