Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SafeHandles #2127

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading