From 23a1275b9f9ae653dd554411830b231e3f748b65 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sat, 30 May 2020 18:31:50 +0200 Subject: [PATCH] Soft memory leak bugfix --- PdfForQtLib/sources/pdfdocument.cpp | 5 +++++ PdfForQtLib/sources/pdfdocument.h | 1 + PdfForQtViewer/pdfviewermainwindow.cpp | 16 +++++++++++----- PdfForQtViewer/pdfviewermainwindow.h | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/PdfForQtLib/sources/pdfdocument.cpp b/PdfForQtLib/sources/pdfdocument.cpp index 5894ce5..66cf2ec 100644 --- a/PdfForQtLib/sources/pdfdocument.cpp +++ b/PdfForQtLib/sources/pdfdocument.cpp @@ -45,6 +45,11 @@ QByteArray PDFObjectStorage::getDecodedStream(const PDFStream* stream) const return PDFStreamFilterStorage::getDecodedStream(stream, std::bind(QOverload::of(&PDFObjectStorage::getObject), this, std::placeholders::_1), getSecurityHandler()); } +PDFDocument::~PDFDocument() +{ + +} + bool PDFDocument::operator==(const PDFDocument& other) const { // Document is considered equal, if storage is equal diff --git a/PdfForQtLib/sources/pdfdocument.h b/PdfForQtLib/sources/pdfdocument.h index 2724a3a..7342a9d 100644 --- a/PdfForQtLib/sources/pdfdocument.h +++ b/PdfForQtLib/sources/pdfdocument.h @@ -391,6 +391,7 @@ class PDFFORQTLIBSHARED_EXPORT PDFDocument public: explicit PDFDocument() = default; + ~PDFDocument(); bool operator==(const PDFDocument& other) const; bool operator!=(const PDFDocument& other) const { return !(*this == other); } diff --git a/PdfForQtViewer/pdfviewermainwindow.cpp b/PdfForQtViewer/pdfviewermainwindow.cpp index 27211f2..655144a 100644 --- a/PdfForQtViewer/pdfviewermainwindow.cpp +++ b/PdfForQtViewer/pdfviewermainwindow.cpp @@ -85,6 +85,7 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget* parent) : m_progress(new pdf::PDFProgress(this)), m_taskbarButton(new QWinTaskbarButton(this)), m_progressTaskbarIndicator(nullptr), + m_futureWatcher(nullptr), m_progressDialog(nullptr), m_isBusy(false), m_isChangingProgressStep(false), @@ -287,7 +288,6 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget* parent) : connect(m_progress, &pdf::PDFProgress::progressStarted, this, &PDFViewerMainWindow::onProgressStarted); connect(m_progress, &pdf::PDFProgress::progressStep, this, &PDFViewerMainWindow::onProgressStep); connect(m_progress, &pdf::PDFProgress::progressFinished, this, &PDFViewerMainWindow::onProgressFinished); - connect(&m_futureWatcher, &QFutureWatcher::finished, this, &PDFViewerMainWindow::onDocumentReadingFinished); connect(this, &PDFViewerMainWindow::queryPasswordRequest, this, &PDFViewerMainWindow::onQueryPasswordRequest, Qt::BlockingQueuedConnection); connect(ui->actionFind, &QAction::triggered, this, [this] { m_toolManager->setActiveTool(m_toolManager->getFindTextTool()); }); @@ -862,7 +862,7 @@ void PDFViewerMainWindow::updateUI(bool fullUpdate) void PDFViewerMainWindow::updateActionsAvailability() { - const bool isBusy = m_futureWatcher.isRunning() || m_isBusy; + const bool isBusy = (m_futureWatcher && m_futureWatcher->isRunning()) || m_isBusy; const bool hasDocument = m_pdfDocument; const bool hasValidDocument = !isBusy && hasDocument; bool canPrint = false; @@ -957,7 +957,9 @@ void PDFViewerMainWindow::openDocument(const QString& fileName) return result; }; m_future = QtConcurrent::run(readDocument); - m_futureWatcher.setFuture(m_future); + m_futureWatcher = new QFutureWatcher(); + connect(m_futureWatcher, &QFutureWatcher::finished, this, &PDFViewerMainWindow::onDocumentReadingFinished); + m_futureWatcher->setFuture(m_future); updateActionsAvailability(); } @@ -966,6 +968,10 @@ void PDFViewerMainWindow::onDocumentReadingFinished() QApplication::restoreOverrideCursor(); AsyncReadingResult result = m_future.result(); + m_future = QFuture(); + m_futureWatcher->deleteLater(); + m_futureWatcher = nullptr; + switch (result.result) { case pdf::PDFDocumentReader::Result::OK: @@ -1122,7 +1128,7 @@ int PDFViewerMainWindow::adjustDpiX(int value) void PDFViewerMainWindow::closeEvent(QCloseEvent* event) { - if (m_futureWatcher.isRunning()) + if (m_futureWatcher && m_futureWatcher->isRunning()) { // Jakub Melka: Do not allow to close the application, if document // reading is running. @@ -1226,7 +1232,7 @@ void PDFViewerMainWindow::updateUndoRedoSettings() void PDFViewerMainWindow::updateUndoRedoActions() { - const bool isBusy = m_futureWatcher.isRunning() || m_isBusy; + const bool isBusy = (m_futureWatcher && m_futureWatcher->isRunning()) || m_isBusy; const bool canUndo = !isBusy && m_undoRedoManager->canUndo(); const bool canRedo = !isBusy && m_undoRedoManager->canRedo(); diff --git a/PdfForQtViewer/pdfviewermainwindow.h b/PdfForQtViewer/pdfviewermainwindow.h index 4b665d9..a8652c2 100644 --- a/PdfForQtViewer/pdfviewermainwindow.h +++ b/PdfForQtViewer/pdfviewermainwindow.h @@ -174,7 +174,7 @@ private: PDFFileInfo m_fileInfo; QFuture m_future; - QFutureWatcher m_futureWatcher; + QFutureWatcher* m_futureWatcher; QProgressDialog* m_progressDialog; bool m_isBusy;