diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-01 19:40:24 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-01 19:40:24 +0100 |
commit | bdf6ac149ec260dab767663419731b302679f458 (patch) | |
tree | b22277e55cf4167e7bd4af92c21f174ee76686ae /backend | |
parent | 221b08e07246e84587b4144eca5564ce9ad3a20a (diff) |
test for memory leaks
Diffstat (limited to 'backend')
-rw-r--r-- | backend/Player.cpp | 4 | ||||
-rw-r--r-- | backend/Player.h | 2 | ||||
-rw-r--r-- | backend/util.cpp | 27 | ||||
-rw-r--r-- | backend/util.h | 8 |
4 files changed, 30 insertions, 11 deletions
diff --git a/backend/Player.cpp b/backend/Player.cpp index 5e0cca9..2de5632 100644 --- a/backend/Player.cpp +++ b/backend/Player.cpp @@ -6,6 +6,10 @@ #include "util.h" Player::Player(Dungeon & dungeon) : dungeon(dungeon) { } +Player::~Player() { + safe_free(this->weapon); + safe_free(this->armor); +} float Player::get_attack() const { if (this->cheating) return 1.f; diff --git a/backend/Player.h b/backend/Player.h index 0dce061..6fbd70d 100644 --- a/backend/Player.h +++ b/backend/Player.h @@ -23,7 +23,7 @@ private: public: Player(Dungeon & dungeon); - virtual ~Player() = default; + virtual ~Player(); public: void take_damage(unsigned int dmg); diff --git a/backend/util.cpp b/backend/util.cpp index afac06e..557df4c 100644 --- a/backend/util.cpp +++ b/backend/util.cpp @@ -1,22 +1,41 @@ +#include <stdio.h> #include <stdlib.h> #include "util.h" +#include "WeaponObject.h" +#include "ArmorObject.h" + +template <> void safe_free(void * & ptr) { if (ptr == nullptr) return; free(ptr); ptr = nullptr; } + +template <> void safe_free(const char * & ptr) { auto x = static_cast<void *>(const_cast<char *>(ptr)); safe_free(x); } -void safe_free(char * & ptr) { - auto x = static_cast<void *>(ptr); - safe_free(x); + +template <> +void safe_free(WeaponObject * & ptr) { + delete ptr; } -void safe_free(FILE * & ptr) { + +template <> +void safe_free(ArmorObject * & ptr) { + delete ptr; +} + + +template <typename T> +void safe_free(T * & ptr) { auto x = static_cast<void *>(ptr); safe_free(x); } +template void safe_free(char * &); +template void safe_free(FILE * &); + diff --git a/backend/util.h b/backend/util.h index 15dca58..d278eb1 100644 --- a/backend/util.h +++ b/backend/util.h @@ -1,11 +1,7 @@ #pragma once -#include <stdio.h> - -void safe_free(void * & ptr); -void safe_free(char * & ptr); -void safe_free(const char * & ptr); -void safe_free(FILE * & ptr); +template <typename T> +void safe_free(T * & ptr); template <typename T> inline T min(const T &, const T &); |