From 9eb1a7cb187ecdcb0af70b28cd346aa73885e6a3 Mon Sep 17 00:00:00 2001 From: Yuriy Puchkov Date: Thu, 15 Oct 2020 13:06:38 +0300 Subject: [PATCH] Fix - memory leaks --- src/core/globalshortcutfilter.cpp | 13 +++++++++++-- src/core/globalshortcutfilter.h | 4 ++++ src/tools/storage/s3/imgs3uploader.cpp | 6 +++++- src/tools/storage/s3/imgs3uploader.h | 1 + src/widgets/historywidget.cpp | 15 +++++++++------ src/widgets/historywidget.h | 4 ++-- 6 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/core/globalshortcutfilter.cpp b/src/core/globalshortcutfilter.cpp index 56534973..57f198ba 100644 --- a/src/core/globalshortcutfilter.cpp +++ b/src/core/globalshortcutfilter.cpp @@ -23,6 +23,8 @@ GlobalShortcutFilter::GlobalShortcutFilter(QObject* parent) : QObject(parent) { + m_history = nullptr; + // Forced Print Screen if (RegisterHotKey(NULL, 1, 0, VK_SNAPSHOT)) { // ok - capture screen @@ -33,6 +35,11 @@ GlobalShortcutFilter::GlobalShortcutFilter(QObject* parent) } } +GlobalShortcutFilter::~GlobalShortcutFilter() +{ + delete m_history; +} + bool GlobalShortcutFilter::nativeEventFilter(const QByteArray& eventType, void* message, long* result) @@ -49,8 +56,10 @@ bool GlobalShortcutFilter::nativeEventFilter(const QByteArray& eventType, // Show screenshots history if (VK_SNAPSHOT == keycode && MOD_SHIFT == modifiers) { - HistoryWidget* pHistory = new HistoryWidget(); - pHistory->show(); + if (m_history == nullptr) { + m_history = new HistoryWidget(); + } + m_history->show(); } // Capture screen diff --git a/src/core/globalshortcutfilter.h b/src/core/globalshortcutfilter.h index 3ed8f10d..83137c84 100644 --- a/src/core/globalshortcutfilter.h +++ b/src/core/globalshortcutfilter.h @@ -20,6 +20,8 @@ #include #include +class HistoryWidget; + class GlobalShortcutFilter : public QObject , public QAbstractNativeEventFilter @@ -27,6 +29,7 @@ class GlobalShortcutFilter Q_OBJECT public: explicit GlobalShortcutFilter(QObject* parent = nullptr); + ~GlobalShortcutFilter(); bool nativeEventFilter(const QByteArray& eventType, void* message, @@ -40,4 +43,5 @@ private: quint32 nativeKeycode(Qt::Key key); bool registerShortcut(quint32 nativeKey, quint32 nativeMods); bool unregisterShortcut(quint32 nativeKey, quint32 nativeMods); + HistoryWidget* m_history; }; diff --git a/src/tools/storage/s3/imgs3uploader.cpp b/src/tools/storage/s3/imgs3uploader.cpp index c16f7860..554ce917 100644 --- a/src/tools/storage/s3/imgs3uploader.cpp +++ b/src/tools/storage/s3/imgs3uploader.cpp @@ -56,6 +56,11 @@ ImgS3Uploader::ImgS3Uploader(QWidget* parent) init(tr("Delete image from S3"), tr("Deleting image...")); } +ImgS3Uploader::~ImgS3Uploader() +{ + clearProxy(); +} + void ImgS3Uploader::init(const QString& title, const QString& label) { m_proxy = nullptr; @@ -87,7 +92,6 @@ QNetworkProxy* ImgS3Uploader::initProxy() if (httpProxyHost.length() > 0) { m_proxy = new QNetworkProxy(); - if (settings->contains("HTTP_PROXY_TYPE")) { switch (settings->value("HTTP_PROXY_TYPE").toInt()) { case 0: diff --git a/src/tools/storage/s3/imgs3uploader.h b/src/tools/storage/s3/imgs3uploader.h index a5938895..8a9ec7aa 100644 --- a/src/tools/storage/s3/imgs3uploader.h +++ b/src/tools/storage/s3/imgs3uploader.h @@ -41,6 +41,7 @@ class ImgS3Uploader : public ImgUploader public: explicit ImgS3Uploader(const QPixmap& capture, QWidget* parent = nullptr); explicit ImgS3Uploader(QWidget* parent = nullptr); + ~ImgS3Uploader(); void upload(); void deleteResource(const QString&, const QString&); diff --git a/src/widgets/historywidget.cpp b/src/widgets/historywidget.cpp index 93afc738..25df2ccd 100644 --- a/src/widgets/historywidget.cpp +++ b/src/widgets/historywidget.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -46,6 +45,11 @@ HistoryWidget::HistoryWidget(QWidget* parent) loadHistory(); } +HistoryWidget::~HistoryWidget() +{ + delete m_notification; +} + void HistoryWidget::loadHistory() { // read history files @@ -140,9 +144,7 @@ void HistoryWidget::addLine(const QString& path, const QString& fileName) // TODO - remove dependency injection (s3 & imgur) if (unpackFileName.type.compare(SCREENSHOT_STORAGE_TYPE_S3) == 0) { if (unpackFileName.token.length() > 0) { - ImgS3Uploader* uploader = new ImgS3Uploader(); - removeItem( - uploader, phbl, unpackFileName.file, unpackFileName.token); + removeItem(phbl, unpackFileName.file, unpackFileName.token); } else { // for compatibility with previous versions and to be able to // remove previous screenshots @@ -176,18 +178,19 @@ void HistoryWidget::addLine(const QString& path, const QString& fileName) m_pVBox->addLayout(phbl); } -void HistoryWidget::removeItem(ImgUploader* imgUploader, - QLayout* pl, +void HistoryWidget::removeItem(QLayout* pl, const QString& fileName, const QString& deleteToken) { hide(); + ImgS3Uploader* imgUploader = new ImgS3Uploader(); imgUploader->show(); imgUploader->deleteResource(fileName, deleteToken); connect(imgUploader, &QWidget::destroyed, this, [=]() { if (imgUploader->resultStatus) { removeLayoutItem(pl); } + imgUploader->deleteLater(); show(); }); } diff --git a/src/widgets/historywidget.h b/src/widgets/historywidget.h index 0d88098a..abd4e0b9 100644 --- a/src/widgets/historywidget.h +++ b/src/widgets/historywidget.h @@ -19,6 +19,7 @@ class HistoryWidget : public QDialog Q_OBJECT public: explicit HistoryWidget(QWidget* parent = nullptr); + ~HistoryWidget(); signals: @@ -26,8 +27,7 @@ private: void loadHistory(); void addLine(const QString&, const QString&); void setEmptyMessage(); - void removeItem(ImgUploader* imgUploader, - QLayout* pl, + void removeItem(QLayout* pl, const QString& s3FileName, const QString& deleteToken); void removeLayoutItem(QLayout* pl);