8000 Garbage collection protection · gitextensions/libgit2sharp@7b38c3c · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b38c3c

Browse files
committed
Garbage collection protection
1 parent b65f966 commit 7b38c3c

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

LibGit2Sharp/Network.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ public virtual void Push(
194194
Ensure.ArgumentNotNull(remote, "remote");
195195
Ensure.ArgumentNotNull(pushRefSpecs, "pushRefSpecs");
196196

197+
// The following local variables are protected from garbage collection
198+
// by a GC.KeepAlive call at the end of the method. Otherwise,
199+
// random crashes during push progress reporting could occur.
200+
PushTransferCallbacks pushTransferCallbacks;
201+
PackbuilderCallbacks packBuilderCallbacks;
202+
NativeMethods.git_push_transfer_progress pushProgress;
203+
NativeMethods.git_packbuilder_progress packBuilderProgress;
204+
197205
// Return early if there is nothing to push.
198206
if (!pushRefSpecs.Any())
199207
{
@@ -221,11 +229,11 @@ public virtual void Push(
221229
// Perform the actual push.
222230
using (PushSafeHandle pushHandle = Proxy.git_push_new(remoteHandle))
223231
{
224-
PushTransferCallbacks pushTransferCallbacks = new PushTransferCallbacks(pushOptions.OnPushTransferProgress);
225-
PackbuilderCallbacks packBuilderCallbacks = new PackbuilderCallbacks(pushOptions.OnPackBuilderProgress);
232+
pushTransferCallbacks = new PushTransferCallbacks(pushOptions.OnPushTransferProgress);
233+
packBuilderCallbacks = new PackbuilderCallbacks(pushOptions.OnPackBuilderProgress);
226234

227-
NativeMethods.git_push_transfer_progress pushProgress = pushTransferCallbacks.GenerateCallback();
228-
NativeMethods.git_packbuilder_progress packBuilderProgress = packBuilderCallbacks.GenerateCallback();
235+
pushProgress = pushTransferCallbacks.GenerateCallback();
236+
packBuilderProgress = packBuilderCallbacks.GenerateCallback();
229237

230238
Proxy.git_push_set_callbacks(pushHandle, pushProgress, packBuilderProgress);
231239

@@ -259,6 +267,11 @@ public virtual void Push(
259267
Proxy.git_remote_disconnect(remoteHandle);
260268
}
261269
}
270+
271+
GC.KeepAlive(pushProgress);
272+
GC.KeepAlive(packBuilderProgress);
273+
GC.KeepAlive(pushTransferCallbacks);
274+
GC.KeepAlive(packBuilderCallbacks);
262275
}
263276

264277
/// <summary>

0 commit comments

Comments
 (0)
0