From 1aa7e560f1fe89908c701f0c63e026a80ab0faf5 Mon Sep 17 00:00:00 2001 From: flamin Date: Mon, 21 Jan 2019 16:02:04 -0500 Subject: [PATCH] Fix all memory leaks reported by valgrind --- src/game.c | 1 + src/stack.c | 1 + tests/card_test.c | 3 +++ tests/frame_test.c | 3 +++ tests/game_test.c | 4 ++++ tests/stack_test.c | 22 ++++++++++++++++------ tests/test_helper_test.c | 31 +++++++++++++++++++++++++++++++ 7 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/game.c b/src/game.c index fefb0bd..0fcc077 100644 --- a/src/game.c +++ b/src/game.c @@ -162,6 +162,7 @@ static void shuffle_deck(struct deck *deck) { for (int i = 0; i < NUMBER_OF_CARDS; i++) { stack_push(&(deck->stock), card[i]); } + free(card); } static void deal_cards(struct deck *deck) { diff --git a/src/stack.c b/src/stack.c index 9d05556..8a2bd3f 100644 --- a/src/stack.c +++ b/src/stack.c @@ -69,6 +69,7 @@ int stack_length(struct stack *stack) { void stack_push(struct stack **stack, struct card *card) { if (card) { if (stack_empty(*stack)) { + card_free((*stack)->card); (*stack)->card = card; } else { /* Allocating by hand because stack_malloc() would diff --git a/tests/card_test.c b/tests/card_test.c index c5abb40..2775cf7 100644 --- a/tests/card_test.c +++ b/tests/card_test.c @@ -25,6 +25,9 @@ void test_card_dup() { assert(card_0 != card_1); assert(cards_equal(card_0, card_1)); + + card_free(card_0); + card_free(card_1); } void test_card_set() { diff --git a/tests/frame_test.c b/tests/frame_test.c index fecae56..1a32c35 100644 --- a/tests/frame_test.c +++ b/tests/frame_test.c @@ -25,6 +25,9 @@ void test_frame_dup() { assert(frame_0 != frame_1); assert(frames_equal(frame_0, frame_1)); + + frame_free(frame_0); + frame_free(frame_1); } void test_frame_set() { diff --git a/tests/game_test.c b/tests/game_test.c index e38e138..f2fc88b 100644 --- a/tests/game_test.c +++ b/tests/game_test.c @@ -406,6 +406,8 @@ void test_move_card_from_stack_empty_stack_to_stack_empty_stack() { assert(destination == new_destination); assert(stacks_equal(destination, destination_duplicate)); + stack_free(origin_duplicate); + stack_free(destination_duplicate); stack_free(origin); stack_free(destination); } @@ -436,6 +438,8 @@ void test_move_card_from_stack_empty_stack_to_non_stack_empty_stack() { assert(destination == new_destination); assert(stacks_equal(destination, destination_duplicate)); + stack_free(origin_duplicate); + stack_free(destination_duplicate); stack_free(origin); stack_free(destination); } diff --git a/tests/stack_test.c b/tests/stack_test.c index 9776af3..9f3434a 100644 --- a/tests/stack_test.c +++ b/tests/stack_test.c @@ -30,6 +30,9 @@ void test_stack_dup() { assert(stack_0 != stack_1); assert(stacks_equal(stack_0, stack_1)); + + stack_free(stack_0); + stack_free(stack_1); } void test_stack_empty_on_stack_empty_stack() { @@ -185,6 +188,7 @@ void test_stack_pop_on_stack_with_one_element() { assert(stack_popped_card == card); stack_free(stack); + card_free(stack_popped_card); } void test_stack_pop_on_stack_with_more_than_one_element() { @@ -207,6 +211,7 @@ void test_stack_pop_on_stack_with_more_than_one_element() { assert(stack_popped_card == card[2]); stack_free(stack); + card_free(stack_popped_card); } void test_stack_reverse_on_stack_empty_stack() { @@ -220,6 +225,7 @@ void test_stack_reverse_on_stack_empty_stack() { assert(stacks_equal(stack_reversed_stack, old_stack)); stack_free(stack); + stack_free(stack_reversed_stack); } void test_stack_reverse_on_stack_with_one_element() { @@ -238,6 +244,7 @@ void test_stack_reverse_on_stack_with_one_element() { assert(stacks_equal(stack_reversed_stack, old_stack)); + stack_free(stack_reversed_stack); stack_free(stack); } @@ -264,12 +271,14 @@ void test_stack_reverse_on_stack_with_more_than_one_element() { assert(stacks_equal(unstack_reversed_stack, old_stack)); + stack_free(unstack_reversed_stack); stack_free(stack_reversed_stack); + stack_free(old_stack); stack_free(stack); } void test_stack_reverse_should_not_change_stack() { - struct stack *stack, *old_stack, *old_stack_address; + struct stack *stack, *stack_reversed_stack_0, *stack_reversed_stack_1; struct card *card[3]; stack_malloc(&stack); @@ -280,13 +289,14 @@ void test_stack_reverse_should_not_change_stack() { card_set(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0); stack_push(&stack, card[i]); } - old_stack_address = stack; - old_stack = stack_dup(stack); - stack_reverse(stack); + stack_reversed_stack_0 = stack_reverse(stack); + stack_reversed_stack_1 = stack_reverse(stack_reversed_stack_0); - assert(stack == old_stack_address); - assert(stacks_equal(stack, old_stack)); + assert(!stacks_equal(stack, stack_reversed_stack_0)); + assert(stacks_equal(stack, stack_reversed_stack_1)); + stack_free(stack_reversed_stack_0); + stack_free(stack_reversed_stack_1); stack_free(stack); } diff --git a/tests/test_helper_test.c b/tests/test_helper_test.c index a16921a..accbba7 100644 --- a/tests/test_helper_test.c +++ b/tests/test_helper_test.c @@ -11,6 +11,8 @@ void test_frames_equal_with_one_null() { frame_malloc(&frame); assert(!frames_equal(frame, NULL)); assert(!frames_equal(NULL, frame)); + + frame_free(frame); } void test_frames_equal_with_two_equivalent_frames() { @@ -23,14 +25,20 @@ void test_frames_equal_with_two_equivalent_frames() { frame_set(frame_1, begin_y, begin_x); assert(frames_equal(frame_0, frame_1)); + + frame_free(frame_0); + frame_free(frame_1); } void test_frames_equal_with_two_frame_pointers_to_the_same_address() { struct frame *frame; frame_malloc(&frame); + frame_init(frame); assert(frames_equal(frame, frame)); + + frame_free(frame); } void test_cards_equal_with_two_nulls() { @@ -55,14 +63,20 @@ void test_cards_equal_with_two_equivalent_cards() { card_set(card_1, ACE, SPADES, EXPOSED, begin_y, begin_x); assert(cards_equal(card_0, card_1)); + + card_free(card_0); + card_free(card_1); } void test_cards_equal_with_two_card_pointers_to_the_same_address() { struct card *card; card_malloc(&card); + card_init(card); assert(cards_equal(card, card)); + + card_free(card); } void test_stacks_equal_with_two_nulls() { @@ -73,8 +87,12 @@ void test_stacks_equal_with_one_null() { struct stack *stack; stack_malloc(&stack); + stack_init(stack); + assert(!stacks_equal(stack, NULL)); assert(!stacks_equal(NULL, stack)); + + stack_free(stack); } void test_stacks_equal_with_two_equivalent_stacks() { @@ -88,10 +106,15 @@ void test_stacks_equal_with_two_equivalent_stacks() { card_set(card_1, ACE, SPADES, EXPOSED, begin_y, begin_x); stack_malloc(&stack_0); stack_malloc(&stack_1); + stack_init(stack_0); + stack_init(stack_1); stack_push(&stack_0, card_0); stack_push(&stack_1, card_1); assert(stacks_equal(stack_0, stack_1)); + + stack_free(stack_0); + stack_free(stack_1); } void test_stacks_equal_with_two_different_stacks() { @@ -105,18 +128,26 @@ void test_stacks_equal_with_two_different_stacks() { card_set(card_1, KING, HEARTS, EXPOSED, begin_y, begin_x); stack_malloc(&stack_0); stack_malloc(&stack_1); + stack_init(stack_0); + stack_init(stack_1); stack_push(&stack_0, card_0); stack_push(&stack_1, card_1); assert(!stacks_equal(stack_0, stack_1)); + + stack_free(stack_0); + stack_free(stack_1); } void test_stacks_equal_with_two_stack_pointers_to_the_same_address() { struct stack *stack; stack_malloc(&stack); + stack_init(stack); assert(stacks_equal(stack, stack)); + + stack_free(stack); } void test_test_helper() {