win32: teach the allocator to deal with crtdbg Move the MSVC C runtime debugging bits into the allocator's global init function.
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
diff --git a/src/alloc.c b/src/alloc.c
index 51c4d80..7bfbbda 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -10,6 +10,11 @@
#include "allocators/stdalloc.h"
#include "allocators/win32_crtdbg.h"
+#if defined(GIT_MSVC_CRTDBG)
+# include "win32/w32_stack.h"
+# include "win32/w32_crtdbg_stacktrace.h"
+#endif
+
git_allocator git__allocator;
static int setup_default_allocator(void)
@@ -21,8 +26,23 @@ static int setup_default_allocator(void)
#endif
}
+#if defined(GIT_MSVC_CRTDBG)
+static void allocator_global_shutdown(void)
+{
+ git_win32__crtdbg_stacktrace_cleanup();
+ git_win32__stack_cleanup();
+}
+#endif
+
int git_allocator_global_init(void)
{
+#if defined(GIT_MSVC_CRTDBG)
+ git_win32__crtdbg_stacktrace_init();
+ git_win32__stack_init();
+
+ git__on_shutdown(allocator_global_shutdown);
+#endif
+
/*
* We don't want to overwrite any allocator which has been set before
* the init function is called.
diff --git a/src/allocators/win32_crtdbg.c b/src/allocators/win32_crtdbg.c
index 1187e2f..c726268 100644
--- a/src/allocators/win32_crtdbg.c
+++ b/src/allocators/win32_crtdbg.c
@@ -9,6 +9,7 @@
#if defined(GIT_MSVC_CRTDBG)
+#include "win32/w32_stack.h"
#include "win32/w32_crtdbg_stacktrace.h"
static void *crtdbg__malloc(size_t len, const char *file, int line)
diff --git a/src/global.c b/src/global.c
index 4abb5e3..d2a25a5 100644
--- a/src/global.c
+++ b/src/global.c
@@ -19,11 +19,7 @@
#include "thread-utils.h"
#include "git2/global.h"
#include "transports/ssh.h"
-
-#if defined(GIT_MSVC_CRTDBG)
#include "win32/w32_stack.h"
-#include "win32/w32_crtdbg_stacktrace.h"
-#endif
git_mutex git__mwindow_mutex;
@@ -72,12 +68,6 @@ static int init_common(void)
size_t i;
int ret;
- /* Initialize the CRT debug allocator first, before our first malloc */
-#if defined(GIT_MSVC_CRTDBG)
- git_win32__crtdbg_stacktrace_init();
- git_win32__stack_init();
-#endif
-
/* Initialize subsystems that have global state */
for (i = 0; i < ARRAY_SIZE(git__init_callbacks); i++)
if ((ret = git__init_callbacks[i]()) != 0)
@@ -200,11 +190,6 @@ int git_libgit2_shutdown(void)
FlsFree(_fls_index);
git_mutex_free(&git__mwindow_mutex);
-
-#if defined(GIT_MSVC_CRTDBG)
- git_win32__crtdbg_stacktrace_cleanup();
- git_win32__stack_cleanup();
-#endif
}
/* Exit the lock */