Commit 31654a34 by Edward Thomson

win32: consolidate leak checking initialization

Move leak check initialization into git_win32_leakcheck_global_init, and
call it on library initialization.
parent cb4b3bdf
......@@ -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.
......
......@@ -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,
......
......@@ -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
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment