[PATCH v2] Add simple memory leak check to all tests.

U. Artie Eoff ullysses.a.eoff at intel.com
Fri Mar 23 10:01:23 PDT 2012


From: "U. Artie Eoff" <ullysses.a.eoff at intel.com>

Wrap all tests with a memory balance check to detect potential
memory leaks.
Fixed a few tests that had memory leaks contained in the tests
themselves.

Signed-off-by: U. Artie Eoff <ullysses.a.eoff at intel.com>
---
 tests/Makefile.am   |    2 +-
 tests/array-test.c  |    2 ++
 tests/map-test.c    |    2 ++
 tests/test-runner.c |   27 +++++++++++++++++++++++++++
 tests/test-runner.h |    5 +++++
 5 files changed, 37 insertions(+), 1 deletions(-)

diff --git a/tests/Makefile.am b/tests/Makefile.am
index cf81c0e..d7d12e0 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -12,5 +12,5 @@ event_loop_test_SOURCES = event-loop-test.c test-runner.c
 AM_CFLAGS = $(GCC_CFLAGS)
 LDADD = $(top_builddir)/src/libwayland-util.la \
 	$(top_builddir)/src/libwayland-server.la \
-	-lrt $(FFI_LIBS)
+	-lrt -ldl $(FFI_LIBS)
 
diff --git a/tests/array-test.c b/tests/array-test.c
index cb62713..2be369f 100644
--- a/tests/array-test.c
+++ b/tests/array-test.c
@@ -133,4 +133,6 @@ TEST(array_for_each)
 	wl_array_for_each(p, &array)
 		assert(*p == elements[i++]);
 	assert(i == 5);
+
+	wl_array_release(&array);
 }
diff --git a/tests/map-test.c b/tests/map-test.c
index 5ef940f..00de337 100644
--- a/tests/map-test.c
+++ b/tests/map-test.c
@@ -90,4 +90,6 @@ TEST(map_remove)
 	l = wl_map_insert_new(&map, WL_MAP_SERVER_SIDE, &d);
 	assert(l == WL_SERVER_ID_START + 1);
 	assert(wl_map_lookup(&map, l) == &d);
+
+	wl_map_release(&map);
 }
diff --git a/tests/test-runner.c b/tests/test-runner.c
index 301c736..321d5c0 100644
--- a/tests/test-runner.c
+++ b/tests/test-runner.c
@@ -20,6 +20,8 @@
  * OF THIS SOFTWARE.
  */
 
+#define _GNU_SOURCE
+
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -27,10 +29,28 @@
 #include <sys/wait.h>
 #include <string.h>
 #include <assert.h>
+#include <dlfcn.h>
 #include "test-runner.h"
 
+static int num_alloc;
+static void* (*sys_malloc)(size_t);
+static void (*sys_free)(void*);
+
 extern const struct test __start_test_section, __stop_test_section;
 
+void* malloc(size_t size)
+{
+	num_alloc++;
+	return sys_malloc(size);
+}
+
+void free(void* mem)
+{
+	if (mem != NULL)
+		num_alloc--;
+	sys_free(mem);
+}
+
 static const struct test *
 find_test(const char *name)
 {
@@ -46,7 +66,10 @@ find_test(const char *name)
 static void
 run_test(const struct test *t)
 {
+	int cur_alloc = num_alloc;
+
 	t->run();
+	assert(("memory leak detected in test.", cur_alloc == num_alloc));
 	exit(EXIT_SUCCESS);
 }
 
@@ -57,6 +80,10 @@ int main(int argc, char *argv[])
 	int total, pass;
 	siginfo_t info;
 
+	/* Load system malloc and free */
+	sys_malloc = dlsym(RTLD_NEXT, "malloc");
+	sys_free = dlsym(RTLD_NEXT, "free");
+
 	if (argc == 2) {
 		t = find_test(argv[1]);
 		if (t == NULL) {
diff --git a/tests/test-runner.h b/tests/test-runner.h
index c1fdff9..238e613 100644
--- a/tests/test-runner.h
+++ b/tests/test-runner.h
@@ -1,3 +1,6 @@
+#ifndef _TEST_RUNNER_H_
+#define _TEST_RUNNER_H_
+
 struct test {
 	const char *name;
 	void (*run)(void);
@@ -12,3 +15,5 @@ struct test {
 	};							\
 								\
 	static void name(void)
+
+#endif
-- 
1.7.7.6



More information about the wayland-devel mailing list