From 9e1072915a0b44fd0d638259627ae933a387eee9 Mon Sep 17 00:00:00 2001 From: Ishan Mittal <123645800+Ishan-Mittal69@users.noreply.github.com> Date: Tue, 26 Aug 2025 16:08:23 +0530 Subject: [PATCH] Update DecoratorPattern.cpp fix: prevent memory leak by adding destructor in CharacterDecorator Previously, only StarPowerUp defined a destructor, which caused leaks when Mario was wrapped with other decorators (e.g. HeightUp, GunPowerUp). Now CharacterDecorator deletes its wrapped Character, ensuring the entire decorator chain is cleaned up when the outermost object is deleted. --- Lecture 13/C++ Code/DecoratorPattern.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Lecture 13/C++ Code/DecoratorPattern.cpp b/Lecture 13/C++ Code/DecoratorPattern.cpp index 927041c..f4ec493 100644 --- a/Lecture 13/C++ Code/DecoratorPattern.cpp +++ b/Lecture 13/C++ Code/DecoratorPattern.cpp @@ -26,7 +26,9 @@ class CharacterDecorator : public Character { CharacterDecorator(Character* c){ this->character = c; } - + virtual ~CharacterDecorator(){ + delete character; // cleans up resources + }; }; // Concrete Decorator: Height-Increasing Power-Up. @@ -58,10 +60,6 @@ class StarPowerUp : public CharacterDecorator { string getAbilities() const override { return character->getAbilities() + " with Star Power (Limited Time)"; } - - ~StarPowerUp() { - cout << "Destroying StarPowerUp Decorator" << endl; - } }; int main() {