Fixed global font leak that could cause crash on program exit
parent
5b820718b1
commit
7f27724a49
|
@ -43,13 +43,6 @@ namespace tgui
|
|||
{
|
||||
public:
|
||||
|
||||
// Move constructor has to be explicitly declared since this class has a destructor
|
||||
Font(const Font&) = default;
|
||||
Font(Font&&) = default;
|
||||
Font& operator=(const Font&) = default;
|
||||
Font& operator=(Font&&) = default;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Default constructor which will set the font to nullptr
|
||||
///
|
||||
|
@ -102,7 +95,27 @@ namespace tgui
|
|||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Destructor
|
||||
// @brief Default copy constructor
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
Font(const Font& other) = default;
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// @brief Default move constructor
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
Font(Font&& other) = default;
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// @brief Copy move assignment operator
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
Font& operator=(const Font& other);
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// @brief Copy move assignment operator
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
Font& operator=(Font&& other);
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// @brief Destructor
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
~Font();
|
||||
|
||||
|
|
|
@ -58,7 +58,7 @@ namespace tgui
|
|||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Sets the default font used by widgets
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
TGUI_API void setGlobalFont(Font font);
|
||||
TGUI_API void setGlobalFont(const Font& font);
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -66,14 +66,14 @@ namespace tgui
|
|||
///
|
||||
/// This function will create the font if there was no global font yet.
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
TGUI_API const Font& getGlobalFont();
|
||||
TGUI_API Font getGlobalFont();
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @internal
|
||||
/// @brief Retrieves the default font used for all new widgets, without construction it when it didn't exist yet
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
TGUI_API const Font& getInternalGlobalFont();
|
||||
TGUI_API const std::shared_ptr<sf::Font>& getInternalGlobalFont();
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#include <TGUI/Loading/Deserializer.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
@ -77,10 +78,44 @@ namespace tgui
|
|||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Font& Font::operator=(const Font& other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
// If this was the last instance that shares the global font then also destroy the global font
|
||||
if (m_font && (m_font == getInternalGlobalFont()) && (m_font.use_count() == 2))
|
||||
setGlobalFont(nullptr);
|
||||
|
||||
m_font = other.m_font;
|
||||
m_id = other.m_id;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Font& Font::operator=(Font&& other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
// If this was the last instance that shares the global font then also destroy the global font
|
||||
if (m_font && (m_font == getInternalGlobalFont()) && (m_font.use_count() == 2))
|
||||
setGlobalFont(nullptr);
|
||||
|
||||
m_font = std::move(other.m_font);
|
||||
m_id = std::move(other.m_id);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Font::~Font()
|
||||
{
|
||||
// If this is the last instance that shares the global font then also destroy the global font
|
||||
if ((*this == getInternalGlobalFont()) && (m_font.use_count() == 2))
|
||||
if (m_font && (m_font == getInternalGlobalFont()) && (m_font.use_count() == 2))
|
||||
setGlobalFont(nullptr);
|
||||
}
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ namespace tgui
|
|||
unsigned int globalTextSize = 13;
|
||||
unsigned int globalDoubleClickTime = 500;
|
||||
std::string globalResourcePath = "";
|
||||
Font globalFont = nullptr;
|
||||
std::shared_ptr<sf::Font> globalFont = nullptr;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -60,24 +60,27 @@ namespace tgui
|
|||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void setGlobalFont(Font font)
|
||||
void setGlobalFont(const Font& font)
|
||||
{
|
||||
globalFont = std::move(font);
|
||||
globalFont = font.getFont();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const Font& getGlobalFont()
|
||||
Font getGlobalFont()
|
||||
{
|
||||
if (!globalFont)
|
||||
setGlobalFont({defaultFontBytes, sizeof(defaultFontBytes)});
|
||||
{
|
||||
globalFont = std::make_shared<sf::Font>();
|
||||
globalFont->loadFromMemory(defaultFontBytes, sizeof(defaultFontBytes));
|
||||
}
|
||||
|
||||
return globalFont;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const Font& getInternalGlobalFont()
|
||||
const std::shared_ptr<sf::Font>& getInternalGlobalFont()
|
||||
{
|
||||
return globalFont;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue