From 92f0b7c216f44e589150bfbb458d981a86d6cdee Mon Sep 17 00:00:00 2001 From: Adrian <39585474+m2q@users.noreply.github.com> Date: Mon, 2 Jul 2018 13:55:57 +0200 Subject: [PATCH] Fixed memory leaks associated with catch2 sections Using smart pointers now --- live-test.sh | 4 ++++ src/bboard/bboard.cpp | 12 +----------- src/bboard/bboard.hpp | 9 ++------- src/bboard/step.cpp | 4 +++- src/main.cpp | 5 +++-- test.sh | 2 +- unit_test/bboard/board_logic.cpp | 21 +++++++++++---------- unit_test/bboard/live_testing.cpp | 7 +++++-- unit_test/bboard/performance_test.cpp | 6 ++++-- unit_test/bboard/step_utility_test.cpp | 16 +++++++++------- 10 files changed, 43 insertions(+), 43 deletions(-) create mode 100755 live-test.sh diff --git a/live-test.sh b/live-test.sh new file mode 100755 index 0000000..eeb294e --- /dev/null +++ b/live-test.sh @@ -0,0 +1,4 @@ +#! /bin/sh +make -s main +make -s test +(cd bin/ && ./test "[live testing]") diff --git a/src/bboard/bboard.cpp b/src/bboard/bboard.cpp index e2184ff..783f807 100644 --- a/src/bboard/bboard.cpp +++ b/src/bboard/bboard.cpp @@ -51,17 +51,8 @@ void State::PutAgentsInCorners(int a0, int a1, int a2, int a3) agents[a2].y = agents[a3].y = BOARD_SIZE - 1; } -State* InitEmpty(int a0, int a1, int a2, int a3) +void InitState(State* result, int a0, int a1, int a2, int a3) { - State* result = new State(); - result->PutAgentsInCorners(a0, a1, a2, a3); - return result; -} - -State* InitState(int a0, int a1, int a2, int a3) -{ - State* result = new State(); - // Randomly put obstacles std::mt19937_64 rng(0x1337); std::uniform_int_distribution intDist(0,6); @@ -75,7 +66,6 @@ State* InitState(int a0, int a1, int a2, int a3) } result->PutAgentsInCorners(a0, a1, a2, a3); - return result; } diff --git a/src/bboard/bboard.hpp b/src/bboard/bboard.hpp index c405d44..ec54578 100644 --- a/src/bboard/bboard.hpp +++ b/src/bboard/bboard.hpp @@ -265,20 +265,15 @@ struct Agent virtual Move act(State* state) = 0; }; -/** - * @brief Same as init state but without obstacles - * @see bboard::InitState - */ -State* InitEmpty(int a0, int a1, int a2, int a3); - /** * @brief InitState Returns an meaningfully initialized state + * @param state State * @param a0 Agent no. that should be at top left * @param a1 Agent no. that should be top right * @param a2 Agent no. that should be bottom right * @param a3 Agent no. that should be bottom left */ -State* InitState(int a0, int a1, int a2, int a3); +void InitState(State* state, int a0, int a1, int a2, int a3); /** * @brief Applies given moves to the given board state. diff --git a/src/bboard/step.cpp b/src/bboard/step.cpp index c1ef218..28bc275 100644 --- a/src/bboard/step.cpp +++ b/src/bboard/step.cpp @@ -25,6 +25,7 @@ void Step(State* state, Move* moves) int rootIdx = 0; int i = rootNumber == 0 ? 0 : roots[0]; // no roots -> start from 0 + // iterates 4 times but the index i jumps around the dependencies for(int _ = 0; _ < AGENT_COUNT; _++, i = dependency[i]) { @@ -41,7 +42,7 @@ void Step(State* state, Move* moves) } else if(m == Move::BOMB) { - state->PlantBomb(i, state->agents[i].x, state->agents[i].y); + //state->PlantBomb(i, state->agents[i].x, state->agents[i].y); } int x = state->agents[i].x; int y = state->agents[i].y; @@ -49,6 +50,7 @@ void Step(State* state, Move* moves) Position desired = destPos[i]; int itemOnDestination = state->board[desired.y][desired.x]; + // check out of bounds if(desired.x < 0 || desired.y < 0 || desired.x >= BOARD_SIZE || diff --git a/src/main.cpp b/src/main.cpp index ab1da35..90dc7c3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,9 @@ int main() { // Init - bboard::State* init = bboard::InitState(0,1,2,3); + std::unique_ptr sx = std::make_unique(); + bboard::State* init = sx.get(); + bboard::InitState(init, 0, 1, 2, 3); init->PutItem(1, 4, bboard::Item::INCRRANGE); init->PutItem(6, 4, bboard::Item::KICK); @@ -17,7 +19,6 @@ int main() agents::RandomAgent r; bboard::Agent* agents[4] = {&r, &r, &r, &r}; bboard::StartGame(init, agents, 500); - delete init; } diff --git a/test.sh b/test.sh index 942d6e4..6867696 100755 --- a/test.sh +++ b/test.sh @@ -1,4 +1,4 @@ #! /bin/sh make -s main make -s test -(cd bin/ && ./test ~"[performance]") +(cd bin/ && ./test ~"[performance]" ~"[live testing]") diff --git a/unit_test/bboard/board_logic.cpp b/unit_test/bboard/board_logic.cpp index ddad6f0..8a4a3ab 100644 --- a/unit_test/bboard/board_logic.cpp +++ b/unit_test/bboard/board_logic.cpp @@ -29,7 +29,10 @@ void PlaceBrick(bboard::State* state, int x, int y) TEST_CASE("Basic Non-Obstacle Movement", "[step function]") { - bboard::State* s = bboard::InitEmpty(0, 1, 2, 3); + std::unique_ptr sx = std::make_unique(); + bboard::State* s = sx.get(); + s->PutAgentsInCorners(0, 1, 2, 3); + bboard::Move id = bboard::Move::IDLE; bboard::Move m[4] = {id, id, id, id}; @@ -52,12 +55,13 @@ TEST_CASE("Basic Non-Obstacle Movement", "[step function]") m[3] = bboard::Move::UP; bboard::Step(s, m); REQUIRE_AGENT(s, 3, 0, 9); - delete s; } TEST_CASE("Basic Obstacle Collision", "[step function]") { - bboard::State* s = bboard::InitEmpty(0, 1, 2, 3); + std::unique_ptr sx = std::make_unique(); + bboard::State* s = sx.get(); + s->PutAgentsInCorners(0, 1, 2, 3); bboard::Move id = bboard::Move::IDLE; bboard::Move m[4] = {id, id, id, id}; @@ -71,13 +75,12 @@ TEST_CASE("Basic Obstacle Collision", "[step function]") m[0] = bboard::Move::DOWN; bboard::Step(s, m); REQUIRE_AGENT(s, 0, 0, 1); - - delete s; } TEST_CASE("Destination Collision", "[step function]") { - bboard::State* s = new bboard::State(); + std::unique_ptr sx = std::make_unique(); + bboard::State* s = sx.get(); bboard::Move id = bboard::Move::IDLE; bboard::Move m[4] = {id, id, id, id}; @@ -124,12 +127,12 @@ TEST_CASE("Destination Collision", "[step function]") REQUIRE_AGENT(s, 2, 1, 0); REQUIRE_AGENT(s, 3, 1, 2); } - delete s; } TEST_CASE("Movement Dependency Handling", "[step function]") { - bboard::State* s = new bboard::State(); + std::unique_ptr sx = std::make_unique(); + bboard::State* s = sx.get(); bboard::Move id = bboard::Move::IDLE; bboard::Move m[4] = {id, id, id, id}; @@ -192,8 +195,6 @@ TEST_CASE("Movement Dependency Handling", "[step function]") REQUIRE_AGENT(s, 1, 1, 1); REQUIRE_AGENT(s, 2, 0, 1); } - - delete s; } TEST_CASE("Bomb Mechanics", "[step function]") diff --git a/unit_test/bboard/live_testing.cpp b/unit_test/bboard/live_testing.cpp index 9f07d3a..7e17b94 100644 --- a/unit_test/bboard/live_testing.cpp +++ b/unit_test/bboard/live_testing.cpp @@ -4,7 +4,11 @@ TEST_CASE("Default Scenario", "[live testing]") { - bboard::State* init = bboard::InitState(0,1,2,3); + std::unique_ptr sx = std::make_unique(); + bboard::State* init = sx.get(); + bboard::InitState(init, 0, 1, 2, 3); + + init->PutAgentsInCorners(0, 1, 2, 3); init->PutItem(1, 4, bboard::Item::INCRRANGE); init->PutItem(6, 4, bboard::Item::KICK); @@ -13,5 +17,4 @@ TEST_CASE("Default Scenario", "[live testing]") agents::RandomAgent r; bboard::Agent* agents[4] = {&r, &r, &r, &r}; bboard::StartGame(init, agents, 500); - delete init; } diff --git a/unit_test/bboard/performance_test.cpp b/unit_test/bboard/performance_test.cpp index 9bbec1c..588faaf 100644 --- a/unit_test/bboard/performance_test.cpp +++ b/unit_test/bboard/performance_test.cpp @@ -32,13 +32,15 @@ void Proxy(bboard::State* s, bboard::Move* m, bboard::Agent* a) } TEST_CASE("Step Function", "[performance]") { - bboard::State* s = bboard::InitState(0,1,2,3); + std::unique_ptr sx = std::make_unique(); + sx.get()->PutAgentsInCorners(0, 1, 2, 3); + agents::RandomAgent a; bboard::Move r = bboard::Move::RIGHT; bboard::Move m[4] = {r, r, r, r}; int times = 100000; - double t = timeMethod(times, Proxy, s, m, &a); + double t = timeMethod(times, Proxy, sx.get(), m, &a); std::cout << std::endl << "bboard::Step(s, m): " diff --git a/unit_test/bboard/step_utility_test.cpp b/unit_test/bboard/step_utility_test.cpp index 013593f..85d6c51 100644 --- a/unit_test/bboard/step_utility_test.cpp +++ b/unit_test/bboard/step_utility_test.cpp @@ -37,7 +37,9 @@ void REQUIRE_POS(bboard::Position p, int x, int y) TEST_CASE("Destination position filling", "[step utilities]") { - bboard::State* s = new bboard::State(); + std::unique_ptr sx = std::make_unique(); + bboard::State* s = sx.get(); + bboard::Move m[4] = { bboard::Move::DOWN, bboard::Move::LEFT, @@ -56,13 +58,13 @@ TEST_CASE("Destination position filling", "[step utilities]") REQUIRE_POS(destPos, 1, 0, 0); REQUIRE_POS(destPos, 2, 3, 0); REQUIRE_POS(destPos, 3, 3, -1); - - delete s; } TEST_CASE("Fix Switch Position", "[step utilities]") { - bboard::State* s = new bboard::State(); + std::unique_ptr sx = std::make_unique(); + bboard::State* s = sx.get(); + bboard::Position des[4]; bboard::Move r = bboard::Move::RIGHT, l = bboard::Move::LEFT; bboard::Move m[4] = {r, r, l, l}; @@ -79,12 +81,13 @@ TEST_CASE("Fix Switch Position", "[step utilities]") REQUIRE_POS(des[1], s->agents[1].x, s->agents[1].y); REQUIRE_POS(des[2], s->agents[2].x, s->agents[2].y); REQUIRE_POS(des[3], 2, 0); - delete s; } TEST_CASE("Dependency Resolving", "[step utilities]") { - bboard::State* s = new bboard::State(); + std::unique_ptr sx = std::make_unique(); + bboard::State* s = sx.get(); + bboard::Move idle = bboard::Move::IDLE; bboard::Move m[4] = {idle, idle, idle, idle}; bboard::Position des[4]; @@ -167,5 +170,4 @@ TEST_CASE("Dependency Resolving", "[step utilities]") REQUIRE_ROOTS(chain, 0, 1); } - delete s; }