From 20cac1737e14df3fd86ef20e49dd9eac8b2fad10 Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Sat, 11 Oct 2025 03:18:34 +0200 Subject: [PATCH] use special valgrind option to detect memory leaks I am hoping that this task fails now --- allocator.h | 3 +-- makefile | 2 +- test_borrow_allocator.c | 4 ++++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/allocator.h b/allocator.h index 289374d..45fe47b 100644 --- a/allocator.h +++ b/allocator.h @@ -93,8 +93,7 @@ allocator_t borrow_allocator_interface(borrow_allocator_t *this); // Using the return a keyword in the statement following this macro will cause // a guaranteed memory leak. #define with_borrow(NAME) \ - borrow_allocator_t UNIQUE = borrow_allocator_create(); \ - for (allocator_t NAME = borrow_allocator_interface(&UNIQUE); !UNIQUE.head; UNIQUE.head = (borrow_allocator_reset(&UNIQUE), (linked_allocation_node_t*) 1)) \ + for (allocator_t NAME = borrow_allocator_interface(&borrow_allocator_create()); !((borrow_allocator_t*)NAME.this)->head; ((borrow_allocator_t*)NAME.this)->head = (borrow_allocator_reset(((borrow_allocator_t*)NAME.this)), (linked_allocation_node_t*) 1)) \ for (int UNIQUE = 0; UNIQUE < 1; UNIQUE++) #ifdef ALLOCATOR_IMPLEMENTATION diff --git a/makefile b/makefile index 691a36d..8571750 100644 --- a/makefile +++ b/makefile @@ -16,7 +16,7 @@ test: echo "Compiling all test files into $(TESTBIN)..."; \ $(CC) $(CFLAGS) _allocator_impl.c $$files -o $(TESTBIN) $(LDFLAGS) || exit 1; \ echo "Running tests..."; \ - valgrind $(TESTBIN); \ + valgrind --trace-children=yes $(TESTBIN); \ fi diff --git a/test_borrow_allocator.c b/test_borrow_allocator.c index 9d6f24b..cd3e88a 100644 --- a/test_borrow_allocator.c +++ b/test_borrow_allocator.c @@ -11,11 +11,15 @@ Test(borrow_allocator, test) { borrow_allocator_reset(&balloc); cr_assert_eq(0, borrow_allocator_count_allocations(&balloc)); borrow_allocator_assert_all_freed(&balloc); + void *foobar = malloc(1000); + (void)foobar; } #define IS_SIZE 900 Test(borrow_allocator, with) { with_borrow(foo) { + with_borrow(bar) { + } int *is = allocator_alloc(foo, sizeof(int) * IS_SIZE); for (int i = 0; i < IS_SIZE; i++) is[i] = i; is = allocator_alloc(foo, sizeof(int) * IS_SIZE);