Skip to content

Commit 31654a3

Browse files
committed
win32: consolidate leak checking initialization
Move leak check initialization into git_win32_leakcheck_global_init, and call it on library initialization.
1 parent cb4b3bd commit 31654a3

File tree

4 files changed

+61
-59
lines changed

4 files changed

+61
-59
lines changed

src/alloc.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
#include "allocators/stdalloc.h"
1212
#include "allocators/win32_crtdbg.h"
1313

14-
#if defined(GIT_MSVC_CRTDBG)
15-
# include "win32/w32_leakcheck.h"
16-
#endif
17-
1814
git_allocator git__allocator;
1915

2016
static int setup_default_allocator(void)
@@ -26,24 +22,8 @@ static int setup_default_allocator(void)
2622
#endif
2723
}
2824

29-
#if defined(GIT_MSVC_CRTDBG)
30-
static void allocator_global_shutdown(void)
31-
{
32-
git_win32_leakcheck_stacktrace_cleanup();
33-
git_win32_leakcheck_stack_cleanup();
34-
}
35-
#endif
36-
3725
int git_allocator_global_init(void)
3826
{
39-
#if defined(GIT_MSVC_CRTDBG)
40-
git_win32_leakcheck_stacktrace_init();
41-
git_win32_leakcheck_stack_init();
42-
43-
if (git_runtime_shutdown_register(allocator_global_shutdown) < 0)
44-
return -1;
45-
#endif
46-
4727
/*
4828
* We don't want to overwrite any allocator which has been set before
4929
* the init function is called.

src/libgit2.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include "transports/http.h"
3333
#include "transports/ssh.h"
3434

35+
#ifdef GIT_WIN32
36+
# include "win32/w32_leakcheck.h"
37+
#endif
38+
3539
#ifdef GIT_OPENSSL
3640
# include <openssl/err.h>
3741
#endif
@@ -64,6 +68,9 @@ static int git_libgit2_settings_global_init(void)
6468
int git_libgit2_init(void)
6569
{
6670
static git_runtime_init_fn init_fns[] = {
71+
#ifdef GIT_WIN32
72+
git_win32_leakcheck_global_init,
73+
#endif
6774
git_allocator_global_init,
6875
git_threadstate_global_init,
6976
git_threads_global_init,

src/win32/w32_leakcheck.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "Dbghelp.h"
1414
#include "win32/posix.h"
1515
#include "hash.h"
16+
#include "runtime.h"
1617

1718
/* Stack frames (for stack tracing, below) */
1819

@@ -31,6 +32,11 @@ int git_win32_leakcheck_stack_set_aux_cb(
3132
return 0;
3233
}
3334

35+
/**
36+
* Load symbol table data. This should be done in the primary
37+
* thread at startup (under a lock if there are other threads
38+
* active).
39+
*/
3440
void git_win32_leakcheck_stack_init(void)
3541
{
3642
if (!g_win32_stack_initialized) {
@@ -41,6 +47,11 @@ void git_win32_leakcheck_stack_init(void)
4147
}
4248
}
4349

50+
/**
51+
* Cleanup symbol table data. This should be done in the
52+
* primary thead at shutdown (under a lock if there are other
53+
* threads active).
54+
*/
4455
void git_win32_leakcheck_stack_cleanup(void)
4556
{
4657
if (g_win32_stack_initialized) {
@@ -399,6 +410,10 @@ static void dump_summary(const char *label)
399410
fflush(stderr);
400411
}
401412

413+
/**
414+
* Initialize our memory leak tracking and de-dup data structures.
415+
* This should ONLY be called by git_libgit2_init().
416+
*/
402417
void git_win32_leakcheck_stacktrace_init(void)
403418
{
404419
InitializeCriticalSection(&g_crtdbg_stacktrace_cs);
@@ -481,6 +496,21 @@ int git_win32_leakcheck_stacktrace_dump(
481496
return r;
482497
}
483498

499+
/**
500+
* Shutdown our memory leak tracking and dump summary data.
501+
* This should ONLY be called by git_libgit2_shutdown().
502+
*
503+
* We explicitly call _CrtDumpMemoryLeaks() during here so
504+
* that we can compute summary data for the leaks. We print
505+
* the stacktrace of each unique leak.
506+
*
507+
* This cleanup does not happen if the app calls exit()
508+
* without calling the libgit2 shutdown code.
509+
*
510+
* This info we print here is independent of any automatic
511+
* reporting during exit() caused by _CRTDBG_LEAK_CHECK_DF.
512+
* Set it in your app if you also want traditional reporting.
513+
*/
484514
void git_win32_leakcheck_stacktrace_cleanup(void)
485515
{
486516
/* At shutdown/cleanup, dump cummulative leak info
@@ -522,4 +552,25 @@ const char *git_win32_leakcheck_stacktrace(int skip, const char *file)
522552
return result;
523553
}
524554

555+
static void git_win32_leakcheck_global_shutdown(void)
556+
{
557+
git_win32_leakcheck_stacktrace_cleanup();
558+
git_win32_leakcheck_stack_cleanup();
559+
}
560+
561+
int git_win32_leakcheck_global_init(void)
562+
{
563+
git_win32_leakcheck_stacktrace_init();
564+
git_win32_leakcheck_stack_init();
565+
566+
return git_runtime_shutdown_register(git_win32_leakcheck_global_shutdown);
567+
}
568+
569+
#else
570+
571+
int git_win32_leakcheck_global_init(void)
572+
{
573+
return 0;
574+
}
575+
525576
#endif

src/win32/w32_leakcheck.h

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
#include "common.h"
1212

13+
/* Initialize the win32 leak checking system. */
14+
int git_win32_leakcheck_global_init(void);
15+
1316
#if defined(GIT_MSVC_CRTDBG)
1417

1518
#include <stdlib.h>
@@ -83,22 +86,6 @@ typedef struct {
8386
void *frames[GIT_WIN32_LEAKCHECK_STACK_MAX_FRAMES];
8487
} git_win32_leakcheck_stack_raw_data;
8588

86-
87-
/**
88-
* Load symbol table data. This should be done in the primary
89-
* thread at startup (under a lock if there are other threads
90-
* active).
91-
*/
92-
void git_win32_leakcheck_stack_init(void);
93-
94-
/**
95-
* Cleanup symbol table data. This should be done in the
96-
* primary thead at shutdown (under a lock if there are other
97-
* threads active).
98-
*/
99-
void git_win32_leakcheck_stack_cleanup(void);
100-
101-
10289
/**
10390
* Capture raw stack trace data for the current process/thread.
10491
*
@@ -172,29 +159,6 @@ int git_win32_leakcheck_stack(
172159
* startup. See tests/main.c for an example.
173160
*/
174161

175-
/**
176-
* Initialize our memory leak tracking and de-dup data structures.
177-
* This should ONLY be called by git_libgit2_init().
178-
*/
179-
void git_win32_leakcheck_stacktrace_init(void);
180-
181-
/**
182-
* Shutdown our memory leak tracking and dump summary data.
183-
* This should ONLY be called by git_libgit2_shutdown().
184-
*
185-
* We explicitly call _CrtDumpMemoryLeaks() during here so
186-
* that we can compute summary data for the leaks. We print
187-
* the stacktrace of each unique leak.
188-
*
189-
* This cleanup does not happen if the app calls exit()
190-
* without calling the libgit2 shutdown code.
191-
*
192-
* This info we print here is independent of any automatic
193-
* reporting during exit() caused by _CRTDBG_LEAK_CHECK_DF.
194-
* Set it in your app if you also want traditional reporting.
195-
*/
196-
void git_win32_leakcheck_stacktrace_cleanup(void);
197-
198162
/**
199163
* Checkpoint options.
200164
*/

0 commit comments

Comments
 (0)