summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2015-04-21 18:21:59 -0400
committerEdward Thomson <ethomson@edwardthomson.com>2015-04-21 18:21:59 -0400
commitaaf42c8df75a78344e3e231260a935d2e034d8eb (patch)
treed617a5c71ce3ea5fe9c05936ea076dfbffcf03a9
parent7636f740b32fcef4888cc3854446f1f98bfc8fb3 (diff)
parent06c985d8647a254000ed172fd888cc853a2c91dd (diff)
downloadlibgit2-aaf42c8df75a78344e3e231260a935d2e034d8eb.tar.gz
Merge pull request #3051 from jeffhostetler/jeffhostetler/memleak_windows_tls_data
Attempt to fix Windows TLS memory leak.
-rw-r--r--src/global.c24
-rw-r--r--src/global.h2
-rw-r--r--src/win32/pthread.c2
3 files changed, 17 insertions, 11 deletions
diff --git a/src/global.c b/src/global.c
index 1f3432d09..f267fbd24 100644
--- a/src/global.c
+++ b/src/global.c
@@ -223,13 +223,10 @@ int git_libgit2_init(void)
static void synchronized_threads_shutdown(void)
{
- void *ptr;
-
/* Shut down any subsystems that have global state */
git__shutdown();
- ptr = TlsGetValue(_tls_index);
- git__global_state_cleanup(ptr);
+ git__free_tls_data();
TlsFree(_tls_index);
git_mutex_free(&git__mwindow_mutex);
@@ -270,15 +267,20 @@ git_global_st *git__global_state(void)
return ptr;
}
-BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
+/**
+ * Free the TLS data associated with this thread.
+ * This should only be used by the thread as it
+ * is exiting.
+ */
+void git__free_tls_data(void)
{
- if (reason == DLL_THREAD_DETACH) {
- void *ptr = TlsGetValue(_tls_index);
- git__global_state_cleanup(ptr);
- git__free(ptr);
- }
+ void *ptr = TlsGetValue(_tls_index);
+ if (!ptr)
+ return;
- return TRUE;
+ git__global_state_cleanup(ptr);
+ git__free(ptr);
+ TlsSetValue(_tls_index, NULL);
}
#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)
diff --git a/src/global.h b/src/global.h
index a89a8d6ab..f56bec46c 100644
--- a/src/global.h
+++ b/src/global.h
@@ -32,4 +32,6 @@ typedef void (*git_global_shutdown_fn)(void);
extern void git__on_shutdown(git_global_shutdown_fn callback);
+extern void git__free_tls_data(void);
+
#endif
diff --git a/src/win32/pthread.c b/src/win32/pthread.c
index ec45ecbe5..a1cc18932 100644
--- a/src/win32/pthread.c
+++ b/src/win32/pthread.c
@@ -20,6 +20,8 @@ static DWORD WINAPI git_win32__threadproc(LPVOID lpParameter)
thread->result = thread->proc(thread->param);
+ git__free_tls_data();
+
return CLEAN_THREAD_EXIT;
}