Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leaks #32

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/engine/box/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ template <typename T> struct Intersection {
std::pair<double, double> intersectionVector = std::make_pair(
0, 0); // Vector start from intersecting, towards intersected
Intersection() = default;
virtual ~Intersection(){};
};

/**
Expand All @@ -40,7 +41,7 @@ template <typename T> class Box {

public:
Box() = default;
~Box() = default;
virtual ~Box(){};

std::pair<double, double>* getPos() { return pos; };
std::vector<std::pair<double, double>> getVertices() {
Expand Down
4 changes: 3 additions & 1 deletion include/engine/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "engine/physics/phxbox.h"
#include "engine/physics/point.h"

#include "macros.h"

namespace Engine {

class InputHandler;
Expand All @@ -38,7 +40,7 @@ class Entity {
public:
Entity();
Entity(std::string name);
~Entity() = default;
virtual ~Entity();

void addPoint();
Point* getPoint();
Expand Down
2 changes: 1 addition & 1 deletion include/engine/input/input_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct Keybind {
class InputHandler {
public:
InputHandler(/* args */) = default;
~InputHandler() = default;
~InputHandler(){};
void setOwner(Entity* entity) { this->owner = entity; };
virtual void doInput(int actionId){};

Expand Down
1 change: 1 addition & 0 deletions include/engine/physics/phxbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class PhxBox {
STATIC ///< Cannot be pushed
};
PhxBox() = default;
~PhxBox(){};

Box<PhxBox>* getBox() { return this->box; };

Expand Down
1 change: 1 addition & 0 deletions include/engine/physics/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Point {

PhxParam getParam();
Point(/* args */) = default;
~Point(){};
Point(PhxParam phxParam) { this->phxParam = phxParam; };

void setMass(double mass) { this->phxParam.mass = mass; };
Expand Down
1 change: 1 addition & 0 deletions include/engine/universe_master.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ typedef std::vector<InputHandler*> InpHdlTable;
class UniverseMaster {
public:
UniverseMaster(/* args */) = default;
~UniverseMaster();

void linkWindow(sf::RenderWindow* p_window) { this->window = p_window; };

Expand Down
6 changes: 6 additions & 0 deletions include/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef MACROS_H
#define MACROS_H

#define IF_NOT_NULL_DELETE(a) (a ? delete a : void())

#endif
6 changes: 6 additions & 0 deletions src/engine/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Engine::Entity::Entity() {
this->sprite = NULL;
}

Engine::Entity::~Entity() {
IF_NOT_NULL_DELETE(this->point);
IF_NOT_NULL_DELETE(this->sprite);
IF_NOT_NULL_DELETE(this->phxbox);
}

Engine::Entity::Entity(std::string name) : Entity() { this->name = name; }

void Engine::Entity::addPoint() {
Expand Down
9 changes: 9 additions & 0 deletions src/engine/universe_master.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@

using namespace Engine;

Engine::UniverseMaster::~UniverseMaster() {
for (Entity* entity : this->allEntity) {
delete entity;
}
for (InputHandler* handle : this->allInpHdl) {
delete handle;
}
}

void Engine::UniverseMaster::addEntity(Entity* entity) {
this->allEntity.push_back(entity);
Point* point = entity->getPoint();
Expand Down