]> git.proxmox.com Git - libgit2.git/blobdiff - src/global.c
Include stacktrace summary in memory leak output.
[libgit2.git] / src / global.c
index f267fbd24c63d7d86343077ae211a5228c7aa3c9..fc6337adc0bfb99a89edb9420a848ae2ecd1e6ae 100644 (file)
 #include "git2/global.h"
 #include "git2/sys/openssl.h"
 #include "thread-utils.h"
-
+#if defined(GIT_MSVC_CRTDBG)
+#include "win32/w32_stack.h"
+#include "win32/w32_crtdbg_stacktrace.h"
+#endif
 
 git_mutex git__mwindow_mutex;
 
 #define MAX_SHUTDOWN_CB 8
 
-#ifdef GIT_SSL
+#ifdef GIT_OPENSSL
 # include <openssl/ssl.h>
 SSL_CTX *git__ssl_ctx;
 # ifdef GIT_THREADS
@@ -57,7 +60,7 @@ static void git__shutdown(void)
        }
 }
 
-#if defined(GIT_THREADS) && defined(GIT_SSL)
+#if defined(GIT_THREADS) && defined(GIT_OPENSSL)
 void openssl_locking_function(int mode, int n, const char *file, int line)
 {
        int lock;
@@ -89,7 +92,7 @@ static void shutdown_ssl_locking(void)
 
 static void init_ssl(void)
 {
-#ifdef GIT_SSL
+#ifdef GIT_OPENSSL
        long ssl_opts = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
 
        /* Older OpenSSL and MacOS OpenSSL doesn't have this */
@@ -116,9 +119,23 @@ static void init_ssl(void)
 #endif
 }
 
+/**
+ * This function aims to clean-up the SSL context which
+ * we allocated.
+ */
+static void uninit_ssl(void)
+{
+#ifdef GIT_OPENSSL
+       if (git__ssl_ctx) {
+               SSL_CTX_free(git__ssl_ctx);
+               git__ssl_ctx = NULL;
+       }
+#endif
+}
+
 int git_openssl_set_locking(void)
 {
-#ifdef GIT_SSL
+#ifdef GIT_OPENSSL
 # ifdef GIT_THREADS
        int num_locks, i;
 
@@ -211,6 +228,11 @@ int git_libgit2_init(void)
 
        /* Only do work on a 0 -> 1 transition of the refcount */
        if ((ret = git_atomic_inc(&git__n_inits)) == 1) {
+#if defined(GIT_MSVC_CRTDBG)
+               git_win32__crtdbg_stacktrace_init();
+               git_win32__stack_init();
+#endif
+
                if (synchronized_threads_init() < 0)
                        ret = -1;
        }
@@ -240,9 +262,15 @@ int git_libgit2_shutdown(void)
        while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
 
        /* Only do work on a 1 -> 0 transition of the refcount */
-       if ((ret = git_atomic_dec(&git__n_inits)) == 0)
+       if ((ret = git_atomic_dec(&git__n_inits)) == 0) {
                synchronized_threads_shutdown();
 
+#if defined(GIT_MSVC_CRTDBG)
+               git_win32__crtdbg_stacktrace_cleanup();
+               git_win32__stack_cleanup();
+#endif
+       }
+
        /* Exit the lock */
        InterlockedExchange(&_mutex, 0);
 
@@ -333,6 +361,7 @@ int git_libgit2_shutdown(void)
 
        /* Shut down any subsystems that have global state */
        git__shutdown();
+       uninit_ssl();
 
        ptr = pthread_getspecific(_tls_key);
        pthread_setspecific(_tls_key, NULL);
@@ -391,6 +420,7 @@ int git_libgit2_shutdown(void)
 
        git__shutdown();
        git__global_state_cleanup(&__state);
+       uninit_ssl();
 
        return 0;
 }