From db002c784f22bd3d0ae1534ada1734dee5a59a01 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 15 Dec 2019 12:27:56 +0100 Subject: [PATCH] Resolve memory issue with Event Hooks. --- src/Battling/EventHooks/EventData.hpp | 1 + src/Battling/EventHooks/EventHook.hpp | 1 + src/Battling/Models/Battle.hpp | 6 +++--- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Battling/EventHooks/EventData.hpp b/src/Battling/EventHooks/EventData.hpp index 5cde29b..f23f364 100644 --- a/src/Battling/EventHooks/EventData.hpp +++ b/src/Battling/EventHooks/EventData.hpp @@ -9,6 +9,7 @@ namespace CreatureLib::Battling { class EventData { public: + virtual ~EventData() = default; virtual EventDataKind GetKind() = 0; }; diff --git a/src/Battling/EventHooks/EventHook.hpp b/src/Battling/EventHooks/EventHook.hpp index 20e0ad5..93717bc 100644 --- a/src/Battling/EventHooks/EventHook.hpp +++ b/src/Battling/EventHooks/EventHook.hpp @@ -19,6 +19,7 @@ namespace CreatureLib::Battling { for (auto listener : _listeners) { listener(eventData); } + delete eventData; } }; } diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index 62a7fea..baaf899 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -23,7 +23,7 @@ namespace CreatureLib::Battling { ChoiceQueue* _currentTurnQueue = nullptr; bool _hasEnded = false; BattleResult _battleResult = BattleResult::Empty(); - EventHook* _eventHook = new EventHook(); + EventHook _eventHook = EventHook(); ScriptSet _volatile; @@ -74,8 +74,8 @@ namespace CreatureLib::Battling { const std::vector& GetSides() const { return _sides; } - void RegisterEventListener(EVENT_HOOK_FUNC(listener)) { this->_eventHook->RegisterListener(listener); } - void TriggerEventListener(EventData* data) { this->_eventHook->TriggerEvent(data); } + void RegisterEventListener(EVENT_HOOK_FUNC(listener)) { this->_eventHook.RegisterListener(listener); } + void TriggerEventListener(EventData* data) { this->_eventHook.TriggerEvent(data); } }; }