Commit 31654a34c9e6a457ef581892ba8775e3f5235f62

Edward Thomson 2020-07-12T18:07:10

win32: consolidate leak checking initialization Move leak check initialization into git_win32_leakcheck_global_init, and call it on library initialization.

diff --git a/src/alloc.c b/src/alloc.c
index eedf85a..a5674c9 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -11,10 +11,6 @@
 #include "allocators/stdalloc.h"
 #include "allocators/win32_crtdbg.h"
 
-#if defined(GIT_MSVC_CRTDBG)
-# include "win32/w32_leakcheck.h"
-#endif
-
 git_allocator git__allocator;
 
 static int setup_default_allocator(void)
@@ -26,24 +22,8 @@ static int setup_default_allocator(void)
 #endif
 }
 
-#if defined(GIT_MSVC_CRTDBG)
-static void allocator_global_shutdown(void)
-{
-	git_win32_leakcheck_stacktrace_cleanup();
-	git_win32_leakcheck_stack_cleanup();
-}
-#endif
-
 int git_allocator_global_init(void)
 {
-#if defined(GIT_MSVC_CRTDBG)
-	git_win32_leakcheck_stacktrace_init();
-	git_win32_leakcheck_stack_init();
-
-	if (git_runtime_shutdown_register(allocator_global_shutdown) < 0)
-		return -1;
-#endif
-
 	/*
 	 * We don't want to overwrite any allocator which has been set before
 	 * the init function is called.
diff --git a/src/libgit2.c b/src/libgit2.c
index 2e9cb85..07414be 100644
--- a/src/libgit2.c
+++ b/src/libgit2.c
@@ -32,6 +32,10 @@
 #include "transports/http.h"
 #include "transports/ssh.h"
 
+#ifdef GIT_WIN32
+# include "win32/w32_leakcheck.h"
+#endif
+
 #ifdef GIT_OPENSSL
 # include <openssl/err.h>
 #endif
@@ -64,6 +68,9 @@ static int git_libgit2_settings_global_init(void)
 int git_libgit2_init(void)
 {
 	static git_runtime_init_fn init_fns[] = {
+#ifdef GIT_WIN32
+		git_win32_leakcheck_global_init,
+#endif
 		git_allocator_global_init,
 		git_threadstate_global_init,
 		git_threads_global_init,
diff --git a/src/win32/w32_leakcheck.c b/src/win32/w32_leakcheck.c
index 4fc3ec4..31803d5 100644
--- a/src/win32/w32_leakcheck.c
+++ b/src/win32/w32_leakcheck.c
@@ -13,6 +13,7 @@
 #include "Dbghelp.h"
 #include "win32/posix.h"
 #include "hash.h"
+#include "runtime.h"
 
 /* Stack frames (for stack tracing, below) */
 
@@ -31,6 +32,11 @@ int git_win32_leakcheck_stack_set_aux_cb(
 	return 0;
 }
 
+/**
+ * Load symbol table data.  This should be done in the primary
+ * thread at startup (under a lock if there are other threads
+ * active).
+ */
 void git_win32_leakcheck_stack_init(void)
 {
 	if (!g_win32_stack_initialized) {
@@ -41,6 +47,11 @@ void git_win32_leakcheck_stack_init(void)
 	}
 }
 
+/**
+ * Cleanup symbol table data.  This should be done in the
+ * primary thead at shutdown (under a lock if there are other
+ * threads active).
+ */
 void git_win32_leakcheck_stack_cleanup(void)
 {
 	if (g_win32_stack_initialized) {
@@ -399,6 +410,10 @@ static void dump_summary(const char *label)
 	fflush(stderr);
 }
 
+/**
+ * Initialize our memory leak tracking and de-dup data structures.
+ * This should ONLY be called by git_libgit2_init().
+ */
 void git_win32_leakcheck_stacktrace_init(void)
 {
 	InitializeCriticalSection(&g_crtdbg_stacktrace_cs);
@@ -481,6 +496,21 @@ int git_win32_leakcheck_stacktrace_dump(
 	return r;
 }
 
+/**
+ * Shutdown our memory leak tracking and dump summary data.
+ * This should ONLY be called by git_libgit2_shutdown().
+ *
+ * We explicitly call _CrtDumpMemoryLeaks() during here so
+ * that we can compute summary data for the leaks. We print
+ * the stacktrace of each unique leak.
+ *
+ * This cleanup does not happen if the app calls exit()
+ * without calling the libgit2 shutdown code.
+ *
+ * This info we print here is independent of any automatic
+ * reporting during exit() caused by _CRTDBG_LEAK_CHECK_DF.
+ * Set it in your app if you also want traditional reporting.
+ */
 void git_win32_leakcheck_stacktrace_cleanup(void)
 {
 	/* At shutdown/cleanup, dump cummulative leak info
@@ -522,4 +552,25 @@ const char *git_win32_leakcheck_stacktrace(int skip, const char *file)
 	return result;
 }
 
+static void git_win32_leakcheck_global_shutdown(void)
+{
+	git_win32_leakcheck_stacktrace_cleanup();
+	git_win32_leakcheck_stack_cleanup();
+}
+
+int git_win32_leakcheck_global_init(void)
+{
+	git_win32_leakcheck_stacktrace_init();
+	git_win32_leakcheck_stack_init();
+
+	return git_runtime_shutdown_register(git_win32_leakcheck_global_shutdown);
+}
+
+#else
+
+int git_win32_leakcheck_global_init(void)
+{
+	return 0;
+}
+
 #endif
diff --git a/src/win32/w32_leakcheck.h b/src/win32/w32_leakcheck.h
index 518f5ad..29bce4e 100644
--- a/src/win32/w32_leakcheck.h
+++ b/src/win32/w32_leakcheck.h
@@ -10,6 +10,9 @@
 
 #include "common.h"
 
+/* Initialize the win32 leak checking system. */
+int git_win32_leakcheck_global_init(void);
+
 #if defined(GIT_MSVC_CRTDBG)
 
 #include <stdlib.h>
@@ -83,22 +86,6 @@ typedef struct {
 	void *frames[GIT_WIN32_LEAKCHECK_STACK_MAX_FRAMES];
 } git_win32_leakcheck_stack_raw_data;
 
-
-/**
- * Load symbol table data.  This should be done in the primary
- * thread at startup (under a lock if there are other threads
- * active).
- */
-void git_win32_leakcheck_stack_init(void);
-
-/**
- * Cleanup symbol table data.  This should be done in the
- * primary thead at shutdown (under a lock if there are other
- * threads active).
- */
-void git_win32_leakcheck_stack_cleanup(void);
-
-
 /**
  * Capture raw stack trace data for the current process/thread.
  *
@@ -173,29 +160,6 @@ int git_win32_leakcheck_stack(
  */
 
 /**
- * Initialize our memory leak tracking and de-dup data structures.
- * This should ONLY be called by git_libgit2_init().
- */
-void git_win32_leakcheck_stacktrace_init(void);
-
-/**
- * Shutdown our memory leak tracking and dump summary data.
- * This should ONLY be called by git_libgit2_shutdown().
- *
- * We explicitly call _CrtDumpMemoryLeaks() during here so
- * that we can compute summary data for the leaks. We print
- * the stacktrace of each unique leak.
- *
- * This cleanup does not happen if the app calls exit()
- * without calling the libgit2 shutdown code.
- *
- * This info we print here is independent of any automatic
- * reporting during exit() caused by _CRTDBG_LEAK_CHECK_DF.
- * Set it in your app if you also want traditional reporting.
- */
-void git_win32_leakcheck_stacktrace_cleanup(void);
-
-/**
  * Checkpoint options.
  */
 typedef enum git_win32_leakcheck_stacktrace_options {