Skip to content

Prevent garbage collection of checkout callbacks. #638

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

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 15 additions & 5 deletions LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -558,24 +558,26 @@ public static string Clone(string sourceUrl, string workdirPath,
CheckoutProgressHandler onCheckoutProgress = null,
Credentials credentials = null)
{
// checkoutCallbacks is protected from garbage collection by a call to GC.KeepAlive
// at the end of the method. gitRemoteCallbacks is a struct, and so doesn't need
// a call to GC.KeepAlive.
CheckoutCallbacks checkoutCallbacks = CheckoutCallbacks.GenerateCheckoutCallbacks(onCheckoutProgress, null);

var callbacks = new RemoteCallbacks(null, onTransferProgress, null, credentials);
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
var remoteCallbacks = new RemoteCallbacks(null, onTransferProgress, null, credentials);
GitRemoteCallbacks gitRemoteCallbacks = remoteCallbacks.GenerateCallbacks();

var cloneOpts = new GitCloneOptions
{
Bare = bare ? 1 : 0,
CheckoutOpts =
{
version = 1,
progress_cb =
checkoutCallbacks.CheckoutProgressCallback,
progress_cb = checkoutCallbacks.CheckoutProgressCallback,
checkout_strategy = checkout
? CheckoutStrategy.GIT_CHECKOUT_SAFE_CREATE
: CheckoutStrategy.GIT_CHECKOUT_NONE
},
RemoteCallbacks = gitCallbacks,
RemoteCallbacks = gitRemoteCallbacks,
};

FilePath repoPath;
Expand All @@ -584,6 +586,8 @@ public static string Clone(string sourceUrl, string workdirPath,
repoPath = Proxy.git_repository_path(repo);
}

GC.KeepAlive(checkoutCallbacks);

return repoPath.Native;
}

Expand Down Expand Up @@ -766,6 +770,10 @@ private void CheckoutTree(
{
CheckoutNotifyHandler onCheckoutNotify = opts.CheckoutNotificationOptions != null ? opts.CheckoutNotificationOptions.CheckoutNotifyHandler : null;
CheckoutNotifyFlags checkoutNotifyFlags = opts.CheckoutNotificationOptions != null ? opts.CheckoutNotificationOptions.NotifyFlags : default(CheckoutNotifyFlags);

// checkoutCallbacks is protected from garbage collection by a call to GC.KeepAlive
// at the end of the method. gitRemoteCallbacks is a struct, and so doesn't need
// a call to GC.KeepAlive.
CheckoutCallbacks checkoutCallbacks = CheckoutCallbacks.GenerateCheckoutCallbacks(opts.OnCheckoutProgress, onCheckoutNotify);

GitStrArrayIn strArray = (paths != null && paths.Count > 0) ? GitStrArrayIn.BuildFrom(ToFilePaths(paths)) : null;
Expand Down Expand Up @@ -793,6 +801,8 @@ private void CheckoutTree(
{
options.Dispose();
}

GC.KeepAlive(checkoutCallbacks);
}

/// <summary>
Expand Down