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

Better safehandles #2130

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
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
11 changes: 9 additions & 2 deletions LibGit2Sharp/BlameHunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Globalization;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;

namespace LibGit2Sharp
{
Expand Down Expand Up @@ -42,12 +43,18 @@ internal unsafe BlameHunk(IRepository repository, git_blame_hunk* rawHunk)
// Signature objects need to have ownership of their native pointers
if (rawHunk->final_signature != null)
{
FinalSignature = new Signature(rawHunk->final_signature);
var ptr = new IntPtr(rawHunk->final_signature);
var signatureHandle = new SignatureHandle(ptr, false);

FinalSignature = new Signature(signatureHandle);
}

if (rawHunk->orig_signature != null)
{
InitialSignature = new Signature(rawHunk->orig_signature);
var ptr = new IntPtr(rawHunk->orig_signature);
var signatureHandle = new SignatureHandle(ptr, false);

InitialSignature = new Signature(signatureHandle);
}
}

Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ internal Configuration(
private void Init(Repository repository)
{
configHandle = Proxy.git_config_new();
RepositoryHandle repoHandle = (repository != null) ? repository.Handle : null;
RepositoryHandle repoHandle = repository?.Handle ?? new RepositoryHandle();

if (repoHandle != null)
if (!repoHandle.IsInvalid)
{
//TODO: push back this logic into libgit2.
// As stated by @carlosmn "having a helper function to load the defaults and then allowing you
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Core/GitFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public delegate int git_filter_stream_fn(
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct git_filter_source
{
public git_repository* repository;
public IntPtr repository;

public char* path;

Expand Down
14 changes: 7 additions & 7 deletions LibGit2Sharp/Core/Handles/Libgit2Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//#define LEAKS_TRACKING

using System;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;

#if LEAKS_IDENTIFYING
namespace LibGit2Sharp.Core
Expand Down Expand Up @@ -83,20 +83,20 @@ namespace LibGit2Sharp.Core.Handles
using System.Globalization;
#endif

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

internal unsafe Libgit2Object(void* ptr, bool owned)
: this(new IntPtr(ptr), owned)
internal unsafe Libgit2Object()
: base(IntPtr.Zero, true)
{
}

internal unsafe Libgit2Object(IntPtr ptr, bool owned)
: base(owned)
: base(IntPtr.Zero, owned)
{
SetHandle(ptr);

Expand All @@ -108,12 +108,12 @@ internal unsafe Libgit2Object(IntPtr ptr, bool owned)
#endif
}

internal IntPtr AsIntPtr() => DangerousGetHandle();
public override bool IsInvalid => handle == IntPtr.Zero;

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

if (leaked)
{
Expand Down
Loading
Loading