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+ */
3440void 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+ */
4455void 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+ */
402417void 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+ */
484514void 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
0 commit comments