aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-01 19:40:24 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-01 19:40:24 +0100
commitbdf6ac149ec260dab767663419731b302679f458 (patch)
treeb22277e55cf4167e7bd4af92c21f174ee76686ae
parent221b08e07246e84587b4144eca5564ce9ad3a20a (diff)
test for memory leaks
-rw-r--r--backend/Player.cpp4
-rw-r--r--backend/Player.h2
-rw-r--r--backend/util.cpp27
-rw-r--r--backend/util.h8
-rw-r--r--frontend/cmd/view.cpp2
5 files changed, 31 insertions, 12 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 &);
diff --git a/frontend/cmd/view.cpp b/frontend/cmd/view.cpp
index 291e321..91eaf94 100644
--- a/frontend/cmd/view.cpp
+++ b/frontend/cmd/view.cpp
@@ -65,7 +65,7 @@ void GameController::cmd_view(string & target) {
lprtf("%s\n", enemy->get_description().c_str());
unsigned enemy_health = enemy->get_health();
- lprtf("%s heeft %s levenspunten.\n", enemy->get_name().c_str(), enemy_health);
+ lprtf("%s heeft %u levenspunten.\n", enemy->get_name().c_str(), enemy_health);
if (!enemy->is_dead())
return;