Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix(chatform, screenshotgrabber): Fixed memory leak
Browse files Browse the repository at this point in the history
Memory for ScreenshotGrabber was allocated, but don't deallocated
  • Loading branch information
Diadlo committed Jun 30, 2016
1 parent 2c49ada commit bf7c62d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
21 changes: 16 additions & 5 deletions src/widget/form/chatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@

ChatForm::ChatForm(Friend* chatFriend)
: f(chatFriend)
, isTyping{false}
, screenshotGrabber(nullptr)
, isTyping(false)
{
Core* core = Core::getInstance();
coreav = core->getAv();
Expand Down Expand Up @@ -208,7 +209,7 @@ void ChatForm::onAttachClicked()
file.close();
continue;
}
long long filesize = file.size();
qint64 filesize = file.size();
file.close();
QFileInfo fi(path);

Expand Down Expand Up @@ -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");
}
Expand All @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions src/widget/form/chatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
#ifndef CHATFORM_H
#define CHATFORM_H

#include "genericchatform.h"
#include "src/core/corestructs.h"
#include <QSet>
#include <QLabel>
#include <QTimer>
#include <QElapsedTimer>

#include "genericchatform.h"
#include "src/core/corestructs.h"
#include "src/widget/tool/screenshotgrabber.h"

class Friend;
class FileTransferInstance;
class QPixmap;
Expand Down Expand Up @@ -112,6 +114,7 @@ private slots:
QAction* loadHistoryAction;
QAction* copyStatusAction;

ScreenshotGrabber* screenshotGrabber;
QHash<uint, FileTransferInstance*> ftransWidgets;
void startCounter();
void stopCounter();
Expand Down
3 changes: 2 additions & 1 deletion src/widget/tool/screenshotgrabber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit bf7c62d

Please sign in to comment.