Commit 55c5f756d80cb762fa21054c7460359424428668

Jeff Hostetler 2015-04-17T09:30:22

Attempt to fix Windows TLS memory leak.

diff --git a/src/global.c b/src/global.c
index 1f3432d..3c97e62 100644
--- a/src/global.c
+++ b/src/global.c
@@ -270,13 +270,21 @@ git_global_st *git__global_state(void)
 	return ptr;
 }
 
+void git__free_thread_global_state(void)
+{
+	void *ptr = TlsGetValue(_tls_index);
+	if (!ptr)
+		return;
+
+	git__global_state_cleanup(ptr);
+	git__free(ptr);
+	TlsSetValue(_tls_index, NULL);
+}
+
 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
 {
-	if (reason == DLL_THREAD_DETACH) {
-		void *ptr = TlsGetValue(_tls_index);
-		git__global_state_cleanup(ptr);
-		git__free(ptr);
-	}
+	if (reason == DLL_THREAD_DETACH)
+		git__free_thread_global_state();
 
 	return TRUE;
 }
diff --git a/src/global.h b/src/global.h
index a89a8d6..9d763b0 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_thread_global_state(void);
+
 #endif
diff --git a/src/win32/pthread.c b/src/win32/pthread.c
index ec45ecb..a6465d4 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_thread_global_state();
+
 	return CLEAN_THREAD_EXIT;
 }