From bf7c62d6fa57b3f8b5078d55bf20372fe6aa47b7 Mon Sep 17 00:00:00 2001 From: Diadlo Date: Thu, 23 Jun 2016 01:47:44 +0300 Subject: [PATCH] fix(chatform, screenshotgrabber): Fixed memory leak Memory for ScreenshotGrabber was allocated, but don't deallocated --- src/widget/form/chatform.cpp | 21 ++++++++++++++++----- src/widget/form/chatform.h | 7 +++++-- src/widget/tool/screenshotgrabber.cpp | 3 ++- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index d6744d92f..e029cc6b9 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -67,7 +67,8 @@ ChatForm::ChatForm(Friend* chatFriend) : f(chatFriend) - , isTyping{false} + , screenshotGrabber(nullptr) + , isTyping(false) { Core* core = Core::getInstance(); coreav = core->getAv(); @@ -208,7 +209,7 @@ void ChatForm::onAttachClicked() file.close(); continue; } - long long filesize = file.size(); + qint64 filesize = file.size(); file.close(); QFileInfo fi(path); @@ -783,9 +784,14 @@ void ChatForm::onScreenshotClicked() void ChatForm::doScreenshot() { - ScreenshotGrabber* screenshotGrabber = new ScreenshotGrabber(this); - connect(screenshotGrabber, &ScreenshotGrabber::screenshotTaken, this, &ChatForm::onScreenshotTaken); + if (!screenshotGrabber) + screenshotGrabber = new ScreenshotGrabber(this); + + connect(screenshotGrabber, &ScreenshotGrabber::screenshotTaken, + this, &ChatForm::onScreenshotTaken); + screenshotGrabber->showGrabber(); + // Create dir for screenshots QDir(Settings::getInstance().getAppDataDirPath()).mkpath("screenshots"); } @@ -807,16 +813,21 @@ void ChatForm::onScreenshotTaken(const QPixmap &pixmap) { QMessageBox::warning(this, tr("Failed to open temporary file", "Temporary file for screenshot"), tr("qTox wasn't able to save the screenshot")); + + delete screenshotGrabber; + screenshotGrabber = nullptr; return; } pixmap.save(&file, "PNG"); - long long filesize = file.size(); + qint64 filesize = file.size(); file.close(); QFileInfo fi(file); emit sendFile(f->getFriendID(), fi.fileName(), fi.filePath(), filesize); + delete screenshotGrabber; + screenshotGrabber = nullptr; } void ChatForm::onLoadHistory() diff --git a/src/widget/form/chatform.h b/src/widget/form/chatform.h index 938a6e8d0..a3f2f90fc 100644 --- a/src/widget/form/chatform.h +++ b/src/widget/form/chatform.h @@ -20,13 +20,15 @@ #ifndef CHATFORM_H #define CHATFORM_H -#include "genericchatform.h" -#include "src/core/corestructs.h" #include #include #include #include +#include "genericchatform.h" +#include "src/core/corestructs.h" +#include "src/widget/tool/screenshotgrabber.h" + class Friend; class FileTransferInstance; class QPixmap; @@ -112,6 +114,7 @@ private: QAction* loadHistoryAction; QAction* copyStatusAction; + ScreenshotGrabber* screenshotGrabber; QHash ftransWidgets; void startCounter(); void stopCounter(); diff --git a/src/widget/tool/screenshotgrabber.cpp b/src/widget/tool/screenshotgrabber.cpp index 22b26329a..36115d7ea 100644 --- a/src/widget/tool/screenshotgrabber.cpp +++ b/src/widget/tool/screenshotgrabber.cpp @@ -132,9 +132,10 @@ void ScreenshotGrabber::acceptRegion() return; qDebug() << "Screenshot accepted, chosen region" << rect; - emit screenshotTaken(this->screenGrab.copy(rect)); + QPixmap pixmap = this->screenGrab.copy(rect); this->window->close(); restoreHiddenWindows(); + emit screenshotTaken(pixmap); } void ScreenshotGrabber::setupScene()