Skip to content
This repository was archived by the owner on Feb 3, 2023. It is now read-only.

Commit 78e57eb

Browse files
committed
Merge pull request libgit2#917 from jeffhostetler/jeffhostetler/git_trace_reference2
Prevent the git_trace callback from being garbage collected
2 parents 347a772 + b109b72 commit 78e57eb

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

LibGit2Sharp/Core/Proxy.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,6 +2959,18 @@ public static GitObjectType git_tag_target_type(GitObjectSafeHandle tag)
29592959

29602960
#region git_trace_
29612961

2962+
/// <summary>
2963+
/// Install/Enable logging inside of LibGit2 to send messages back to LibGit2Sharp.
2964+
///
2965+
/// Since the given callback will be passed into and retained by C code,
2966+
/// it is very important that you pass an actual delegate here (and don't
2967+
/// let the compiler create/cast a temporary one for you). Furthermore, you
2968+
/// must hold a reference to this delegate until you turn off logging.
2969+
///
2970+
/// This callback is unlike other callbacks because logging persists in the
2971+
/// process until disabled; in contrast, most callbacks are only defined for
2972+
/// the duration of the down-call.
2973+
/// </summary>
29622974
public static void git_trace_set(LogLevel level, NativeMethods.git_trace_cb callback)
29632975
{
29642976
using (ThreadAffinity())

LibGit2Sharp/GlobalSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static LogConfiguration LogConfiguration
9797
}
9898
else
9999
{
100-
Proxy.git_trace_set(value.Level, value.GitTraceHandler);
100+
Proxy.git_trace_set(value.Level, value.GitTraceCallback);
101101

102102
Log.Write(LogLevel.Info, "Logging enabled at level {0}", value.Level);
103103
}

LibGit2Sharp/LogConfiguration.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public LogConfiguration(LogLevel level, LogHandler handler)
2727

2828
Level = level;
2929
Handler = handler;
30+
31+
// Explicitly create (and hold a reference to) a callback-delegate to wrap GitTraceHandler().
32+
GitTraceCallback = GitTraceHandler;
3033
}
3134

3235
private LogConfiguration()
@@ -35,8 +38,14 @@ private LogConfiguration()
3538

3639
internal LogLevel Level { get; private set; }
3740
internal LogHandler Handler { get; private set; }
41+
internal NativeMethods.git_trace_cb GitTraceCallback { get; private set; }
3842

39-
internal void GitTraceHandler(LogLevel level, IntPtr msg)
43+
/// <summary>
44+
/// This private method will be called from LibGit2 (from C code via
45+
/// the GitTraceCallback delegate) to route LibGit2 log messages to
46+
/// the same LogHandler as LibGit2Sharp messages.
47+
/// </summary>
48+
private void GitTraceHandler(LogLevel level, IntPtr msg)
4049
{
4150
string message = LaxUtf8Marshaler.FromNative(msg);
4251
Handler(level, message);

0 commit comments

Comments
 (0)