From 20f90222d1215bdeb7fd282cfe689cbfc1feea5c Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 7 Mar 2021 18:20:42 +0100 Subject: [PATCH] Fix memory issues. --- .../AngelScript/AngelScriptItemUseScript.hpp | 6 ++++++ .../AngelScript/AngelScriptResolver.hpp | 3 +++ tests/ScriptTests/ItemUseScriptTests.cpp | 11 +++-------- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/ScriptResolving/AngelScript/AngelScriptItemUseScript.hpp b/src/ScriptResolving/AngelScript/AngelScriptItemUseScript.hpp index 61a020e..72c70c9 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptItemUseScript.hpp +++ b/src/ScriptResolving/AngelScript/AngelScriptItemUseScript.hpp @@ -11,6 +11,12 @@ public: AngelScriptItemUseScript(asIScriptObject* scriptObject, const AngelScriptResolver* resolver) : _scriptObject(scriptObject), _resolver(resolver) {} + ~AngelScriptItemUseScript(){ + if (_scriptObject != nullptr){ + _scriptObject->Release(); + } + } + [[nodiscard]] bool IsItemUsable() const override; [[nodiscard]] bool IsCreatureUseItem() const override; bool IsUseValidForCreature(CreatureLib::Battling::Creature* creature) const override; diff --git a/src/ScriptResolving/AngelScript/AngelScriptResolver.hpp b/src/ScriptResolving/AngelScript/AngelScriptResolver.hpp index cf835d2..ed24b8d 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptResolver.hpp +++ b/src/ScriptResolving/AngelScript/AngelScriptResolver.hpp @@ -34,6 +34,9 @@ private: public: ~AngelScriptResolver() override { + for (const auto& ius : _itemUseScripts) { + delete ius.second; + } delete _contextPool; for (const auto& category : _typeDatabase) { for (const auto& type : category.second) { diff --git a/tests/ScriptTests/ItemUseScriptTests.cpp b/tests/ScriptTests/ItemUseScriptTests.cpp index c3207b7..e0e3f47 100644 --- a/tests/ScriptTests/ItemUseScriptTests.cpp +++ b/tests/ScriptTests/ItemUseScriptTests.cpp @@ -26,6 +26,9 @@ bool IsPokemonUseItem() override { static AngelScriptResolver* _resolverCache = nullptr; static AngelScriptResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) { + if (_resolverCache != nullptr) { + delete _resolverCache; + } _resolverCache = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); _resolverCache->Initialize(mainLib); for (auto kv : _scripts) { @@ -53,8 +56,6 @@ TEST_CASE("Invoke isItemUsable item use script function on empty class") { auto script = GetScript(mainLib, "blankClass"_cnc); REQUIRE(script != nullptr); REQUIRE_FALSE(script->IsItemUsable()); - - delete script; } TEST_CASE("Invoke isItemUsable item use script function") { auto mainLib = TestLibrary::GetLibrary(); @@ -62,8 +63,6 @@ TEST_CASE("Invoke isItemUsable item use script function") { auto script = GetScript(mainLib, "isItemUsable"_cnc); REQUIRE(script != nullptr); REQUIRE(script->IsItemUsable()); - - delete script; } TEST_CASE("Invoke isPokemonUseItem item use script function on empty class") { @@ -72,8 +71,6 @@ TEST_CASE("Invoke isPokemonUseItem item use script function on empty class") { auto script = GetScript(mainLib, "blankClass"_cnc); REQUIRE(script != nullptr); REQUIRE_FALSE(script->IsCreatureUseItem()); - - delete script; } TEST_CASE("Invoke isPokemonUseItem item use script function") { auto mainLib = TestLibrary::GetLibrary(); @@ -81,8 +78,6 @@ TEST_CASE("Invoke isPokemonUseItem item use script function") { auto script = GetScript(mainLib, "isPokemonUseItem"_cnc); REQUIRE(script != nullptr); REQUIRE(script->IsCreatureUseItem()); - - delete script; } #endif \ No newline at end of file