LogManager: Stop using manual memory management

This fixes a memory leak that would occur when the Android frontend
calls LogManager::Init more than once in order to reload settings.

Note that the log window listener is now owned by LogManager instead of
by the frontend, making it consistent with the other log listeners.
This commit is contained in:
JosJuice
2025-05-02 13:56:44 +02:00
parent b566e81644
commit c8be819711
4 changed files with 39 additions and 24 deletions

View File

@ -52,15 +52,16 @@ LogWidget::LogWidget(QWidget* parent) : QDockWidget(parent), m_timer(new QTimer(
connect(&Settings::Instance(), &Settings::DebugFontChanged, this, &LogWidget::UpdateFont);
Common::Log::LogManager::GetInstance()->RegisterListener(LogListener::LOG_WINDOW_LISTENER, this);
Common::Log::LogManager::GetInstance()->RegisterListener(
Common::Log::LogListener::LOG_WINDOW_LISTENER, std::make_unique<LogListenerImpl>(this));
}
LogWidget::~LogWidget()
{
SaveSettings();
Common::Log::LogManager::GetInstance()->RegisterListener(LogListener::LOG_WINDOW_LISTENER,
nullptr);
Common::Log::LogManager::GetInstance()->RegisterListener(
Common::Log::LogListener::LOG_WINDOW_LISTENER, nullptr);
}
void LogWidget::UpdateLog()

View File

@ -18,7 +18,7 @@ class QPlainTextEdit;
class QPushButton;
class QTimer;
class LogWidget final : public QDockWidget, Common::Log::LogListener
class LogWidget final : public QDockWidget
{
Q_OBJECT
public:
@ -29,6 +29,23 @@ protected:
void closeEvent(QCloseEvent*) override;
private:
// LogListener instances are owned by LogManager, so we can't make LogWidget inherit from
// LogListener, since Qt should be in control of LogWidget's lifetime. Instead we have
// this LogListenerImpl class to act as an adapter.
class LogListenerImpl final : public Common::Log::LogListener
{
public:
explicit LogListenerImpl(LogWidget* log_widget) : m_log_widget(log_widget) {}
private:
void Log(Common::Log::LogLevel level, const char* text) override
{
m_log_widget->Log(level, text);
}
LogWidget* m_log_widget;
};
void UpdateLog();
void UpdateFont();
void CreateWidgets();
@ -36,7 +53,7 @@ private:
void LoadSettings();
void SaveSettings();
void Log(Common::Log::LogLevel level, const char* text) override;
void Log(Common::Log::LogLevel level, const char* text);
// Log
QCheckBox* m_log_wrap;