Commit 319ff349039dc73f5fe099f83f198fb65782414f

Edward Thomson 2021-05-18T20:49:40

Merge pull request #5892 from libgit2/ethomson/memleak tests: clean up memory leak, fail on leak for win32

diff --git a/src/win32/w32_leakcheck.c b/src/win32/w32_leakcheck.c
index ef17004..5c8425b 100644
--- a/src/win32/w32_leakcheck.c
+++ b/src/win32/w32_leakcheck.c
@@ -558,6 +558,11 @@ static void git_win32_leakcheck_global_shutdown(void)
 	git_win32_leakcheck_stack_cleanup();
 }
 
+bool git_win32_leakcheck_has_leaks(void)
+{
+	return (g_transient_count_total_leaks > 0);
+}
+
 int git_win32_leakcheck_global_init(void)
 {
 	git_win32_leakcheck_stacktrace_init();
diff --git a/src/win32/w32_leakcheck.h b/src/win32/w32_leakcheck.h
index 7da03ab..cb45e36 100644
--- a/src/win32/w32_leakcheck.h
+++ b/src/win32/w32_leakcheck.h
@@ -21,6 +21,8 @@ int git_win32_leakcheck_global_init(void);
 #include "git2/errors.h"
 #include "strnlen.h"
 
+bool git_win32_leakcheck_has_leaks(void);
+
 /* Stack frames (for stack tracing, below) */
 
 /**
diff --git a/tests/main.c b/tests/main.c
index 00b2bae..207a6a8 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -1,6 +1,10 @@
 #include "clar_libgit2.h"
 #include "clar_libgit2_trace.h"
 
+#ifdef GIT_WIN32_LEAKCHECK
+# include "win32/w32_leakcheck.h"
+#endif
+
 #ifdef _WIN32
 int __cdecl main(int argc, char *argv[])
 #else
@@ -29,6 +33,11 @@ int main(int argc, char *argv[])
 	cl_global_trace_disable();
 	git_libgit2_shutdown();
 
+#ifdef GIT_WIN32_LEAKCHECK
+	if (git_win32_leakcheck_has_leaks())
+		res = res || 1;
+#endif
+
 	at_exit_cmd = getenv("CLAR_AT_EXIT");
 	if (at_exit_cmd != NULL) {
 		int at_exit = system(at_exit_cmd);
diff --git a/tests/refs/races.c b/tests/refs/races.c
index 9134bf9..4767893 100644
--- a/tests/refs/races.c
+++ b/tests/refs/races.c
@@ -31,10 +31,12 @@ void test_refs_races__create_matching_zero_old(void)
 	git_oid_fromstr(&zero_id, "0000000000000000000000000000000000000000");
 
 	cl_git_fail(git_reference_create_matching(&ref, g_repo, refname, &id, 1, &zero_id, NULL));
+	git_reference_free(ref);
 
 	cl_git_pass(git_reference_create_matching(&ref, g_repo, other_refname, &id, 1, &zero_id, NULL));
-	cl_git_fail(git_reference_create_matching(&ref, g_repo, other_refname, &id, 1, &zero_id, NULL));
+	git_reference_free(ref);
 
+	cl_git_fail(git_reference_create_matching(&ref, g_repo, other_refname, &id, 1, &zero_id, NULL));
 	git_reference_free(ref);
 }