win32: consolidate leak checking initialization Move leak check initialization into git_win32_leakcheck_global_init, and call it on library initialization.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
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 {