From 5715219816d725d26e60d6df580c94336a9191ad Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Tue, 15 Oct 2024 12:17:29 +0200 Subject: [PATCH] [fix][cleanup] Use `std::unique_ptr` for `TextField`, and so fix memoryleak. --- include/guisan/widgets/textfield.hpp | 8 +++++++- src/widgets/textfield.cpp | 6 ++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/guisan/widgets/textfield.hpp b/include/guisan/widgets/textfield.hpp index 7f7fe9a..cc6096c 100644 --- a/include/guisan/widgets/textfield.hpp +++ b/include/guisan/widgets/textfield.hpp @@ -62,6 +62,7 @@ #include "guisan/platform.hpp" #include "guisan/widget.hpp" +#include #include namespace gcn @@ -90,6 +91,11 @@ namespace gcn */ TextField(const std::string& text); + /** + * Destructor. + */ + ~TextField() override; + /** * Sets the text of the text field. * @@ -190,7 +196,7 @@ namespace gcn /** * Holds the text of the text field. */ - Text* mText = nullptr; + std::unique_ptr mText; /** * Holds the amount scrolled in x. If a user types more characters than diff --git a/src/widgets/textfield.cpp b/src/widgets/textfield.cpp index 23f46c2..eb20d1e 100644 --- a/src/widgets/textfield.cpp +++ b/src/widgets/textfield.cpp @@ -68,7 +68,7 @@ namespace gcn { - TextField::TextField() : mText(new Text()) + TextField::TextField() : mText(std::make_unique()) { setFocusable(true); @@ -78,7 +78,7 @@ namespace gcn adjustHeight(); } - TextField::TextField(const std::string& text) : mText(new Text(text)) + TextField::TextField(const std::string& text) : mText(std::make_unique(text)) { setFocusable(true); @@ -88,6 +88,8 @@ namespace gcn adjustSize(); } + TextField::~TextField() = default; + void TextField::setText(const std::string& text) { mText->setRow(0, text);