From 504d231429e2a5d2b31a876645b590bb4a6a03f6 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 30 Oct 2024 16:43:22 +0100 Subject: WIP battle the memory leaks --- backend/Dungeon.cpp | 11 +++++++++++ backend/Dungeon.h | 4 ++++ backend/Location.cpp | 4 ++++ backend/util.h | 5 +++++ backend/util.hpp | 11 +++++++++++ 5 files changed, 35 insertions(+) create mode 100644 backend/util.hpp (limited to 'backend') diff --git a/backend/Dungeon.cpp b/backend/Dungeon.cpp index 3de2f11..66a1fa3 100644 --- a/backend/Dungeon.cpp +++ b/backend/Dungeon.cpp @@ -1,5 +1,16 @@ +#include "Location.h" #include "Dungeon.h" #include "RNG.h" +#include "util.h" + + +Dungeon::Dungeon() { + +} + +Dungeon::~Dungeon() { + safe_free(this->locations); +} void Dungeon::update() { diff --git a/backend/Dungeon.h b/backend/Dungeon.h index 5740eb6..eaca6c3 100644 --- a/backend/Dungeon.h +++ b/backend/Dungeon.h @@ -5,6 +5,10 @@ class Location; class Dungeon { +public: + Dungeon(); + ~Dungeon(); + public: void update(); void add_location(Location *); diff --git a/backend/Location.cpp b/backend/Location.cpp index 9add524..31df1e3 100644 --- a/backend/Location.cpp +++ b/backend/Location.cpp @@ -2,6 +2,8 @@ #include "Location.h" #include "ListIterator.h" +#include "Enemy.h" +#include "Object.h" #include "util.h" Location::Location(const char * name, const char * description) { @@ -12,6 +14,8 @@ Location::Location(const char * name, const char * description) { Location::~Location() { safe_free(this->name); safe_free(this->description); + safe_free(this->enemies); + safe_free(this->objects); } void Location::set_name(const char * name) { diff --git a/backend/util.h b/backend/util.h index 1889698..44d9a49 100644 --- a/backend/util.h +++ b/backend/util.h @@ -1,6 +1,11 @@ #pragma once +#include "List.h" + void safe_free(void * & ptr); void safe_free(const char * & ptr); +template +void safe_free(List & ptr_list); +#include "util.hpp" diff --git a/backend/util.hpp b/backend/util.hpp new file mode 100644 index 0000000..2d9e76a --- /dev/null +++ b/backend/util.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include "util.h" + +template +void safe_free(List & ptr_list) { + for (T * obj : ptr_list) + delete obj; + ptr_list.clear(); +} + -- cgit v1.2.3