Merge pull request #57 from namecheap/bugfix/memory-leaks-check

Fix - memory leaks
This commit is contained in:
Yurii Puchkov
2020-10-15 05:24:12 -07:00
committed by GitHub
6 changed files with 32 additions and 11 deletions

View File

@@ -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

View File

@@ -20,6 +20,8 @@
#include <QAbstractNativeEventFilter>
#include <QObject>
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;
};

View File

@@ -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:

View File

@@ -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&);

View File

@@ -10,7 +10,6 @@
#include <QDesktopServices>
#include <QDesktopWidget>
#include <QFileInfo>
#include <QHBoxLayout>
#include <QIcon>
#include <QLabel>
#include <QLayoutItem>
@@ -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();
});
}

View File

@@ -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);