Skip to content

Commit

Permalink
Fixed memory leaks associated with catch2 sections
Browse files Browse the repository at this point in the history
Using smart pointers now
  • Loading branch information
dist1ll committed Jul 2, 2018
1 parent fd2d69a commit 92f0b7c
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 43 deletions.
4 changes: 4 additions & 0 deletions live-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! /bin/sh
make -s main
make -s test
(cd bin/ && ./test "[live testing]")
12 changes: 1 addition & 11 deletions src/bboard/bboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> intDist(0,6);
Expand All @@ -75,7 +66,6 @@ State* InitState(int a0, int a1, int a2, int a3)
}

result->PutAgentsInCorners(a0, a1, a2, a3);
return result;
}


Expand Down
9 changes: 2 additions & 7 deletions src/bboard/bboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion src/bboard/step.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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])
{
Expand All @@ -41,14 +42,15 @@ 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;

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 ||
Expand Down
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
int main()
{
// Init
bboard::State* init = bboard::InitState(0,1,2,3);
std::unique_ptr<bboard::State> sx = std::make_unique<bboard::State>();
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);
Expand All @@ -17,7 +19,6 @@ int main()
agents::RandomAgent r;
bboard::Agent* agents[4] = {&r, &r, &r, &r};
bboard::StartGame(init, agents, 500);
delete init;
}


2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /bin/sh
make -s main
make -s test
(cd bin/ && ./test ~"[performance]")
(cd bin/ && ./test ~"[performance]" ~"[live testing]")
21 changes: 11 additions & 10 deletions unit_test/bboard/board_logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bboard::State> sx = std::make_unique<bboard::State>();
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};

Expand All @@ -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<bboard::State> sx = std::make_unique<bboard::State>();
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};
Expand All @@ -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<bboard::State> sx = std::make_unique<bboard::State>();
bboard::State* s = sx.get();

bboard::Move id = bboard::Move::IDLE;
bboard::Move m[4] = {id, id, id, id};
Expand Down Expand Up @@ -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<bboard::State> sx = std::make_unique<bboard::State>();
bboard::State* s = sx.get();

bboard::Move id = bboard::Move::IDLE;
bboard::Move m[4] = {id, id, id, id};
Expand Down Expand Up @@ -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]")
Expand Down
7 changes: 5 additions & 2 deletions unit_test/bboard/live_testing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

TEST_CASE("Default Scenario", "[live testing]")
{
bboard::State* init = bboard::InitState(0,1,2,3);
std::unique_ptr<bboard::State> sx = std::make_unique<bboard::State>();
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);
Expand All @@ -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;
}
6 changes: 4 additions & 2 deletions unit_test/bboard/performance_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bboard::State> sx = std::make_unique<bboard::State>();
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): "
Expand Down
16 changes: 9 additions & 7 deletions unit_test/bboard/step_utility_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bboard::State> sx = std::make_unique<bboard::State>();
bboard::State* s = sx.get();

bboard::Move m[4] =
{
bboard::Move::DOWN, bboard::Move::LEFT,
Expand All @@ -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<bboard::State> sx = std::make_unique<bboard::State>();
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};
Expand All @@ -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<bboard::State> sx = std::make_unique<bboard::State>();
bboard::State* s = sx.get();

bboard::Move idle = bboard::Move::IDLE;
bboard::Move m[4] = {idle, idle, idle, idle};
bboard::Position des[4];
Expand Down Expand Up @@ -167,5 +170,4 @@ TEST_CASE("Dependency Resolving", "[step utilities]")

REQUIRE_ROOTS(chain, 0, 1);
}
delete s;
}

3 comments on commit 92f0b7c

@AdamStelmaszczyk
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out valgrind, great tool. I'm checking for mem leaks with it in every build, configuration is in .travis.yml

@AdamStelmaszczyk
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are making good progress, keep it up :)

@dist1ll
Copy link
Owner Author

@dist1ll dist1ll commented on 92f0b7c Jul 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much!

Yup, I noticed you were using valgrind (still learning C++ hehe) so I tried it as well.

Super confusing lol. But it helped a bit

Please sign in to comment.