Skip to content

Commit

Permalink
[fix][cleanup] Use std::unique_ptr for TextBox, and so fix memory…
Browse files Browse the repository at this point in the history
…leak.
  • Loading branch information
Jarod42 committed Oct 15, 2024
1 parent e50b3bf commit 9e3b776
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
15 changes: 10 additions & 5 deletions include/guisan/widgets/textbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@
#ifndef GCN_TEXTBOX_HPP
#define GCN_TEXTBOX_HPP

#include <ctime>
#include <string>
#include <vector>

#include "guisan/keylistener.hpp"
#include "guisan/mouselistener.hpp"
#include "guisan/platform.hpp"
#include "guisan/widget.hpp"

#include <memory>
#include <string>
#include <vector>

namespace gcn
{
class Text;
Expand All @@ -91,6 +91,11 @@ namespace gcn
*/
TextBox(const std::string& text);

/**
* Destructor.
*/
~TextBox() override;

/**
* Sets the text of the text box.
*
Expand Down Expand Up @@ -272,7 +277,7 @@ namespace gcn
/**
* Holds the text of the text box.
*/
Text* mText = nullptr;
std::unique_ptr<Text> mText;

/**
* True if the text box is editable, false otherwise.
Expand Down
4 changes: 3 additions & 1 deletion src/widgets/textbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace gcn
TextBox::TextBox() : TextBox("")
{}

TextBox::TextBox(const std::string& text) : mText(new Text(text))
TextBox::TextBox(const std::string& text) : mText(std::make_unique<Text>(text))
{
setFocusable(true);

Expand All @@ -81,6 +81,8 @@ namespace gcn
setFrameSize(1);
}

TextBox::~TextBox() = default;

void TextBox::setText(const std::string& text)
{
mText->setContent(text);
Expand Down

0 comments on commit 9e3b776

Please sign in to comment.