diff options
author | Nicolas Deherly <nicolas.deherly@navblue.aero> | 2020-09-28 16:30:30 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-04-21 08:54:32 +0000 |
commit | ef123933c9537ce1418f0e293d7dbf22f7af9e49 (patch) | |
tree | 31635eab8bc58a0cbbcbe326a0c050b9912523b0 | |
parent | 372dce37231c9246e73f4cefada69071380e48e8 (diff) |
Fix memory management in QPdfDocument functions
All pages that are opened have to be closed, in order to avoid a memory leak.
Change-Id: I15c12b2f1b389638d5fe0a58599d9c410a033652
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
(cherry picked from commit 77c489ed4ceb490f33b805e7ba4cd34ef9191db7)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/pdf/qpdfdocument.cpp | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/src/pdf/qpdfdocument.cpp b/src/pdf/qpdfdocument.cpp index e4ec363ce..c07ec90c4 100644 --- a/src/pdf/qpdfdocument.cpp +++ b/src/pdf/qpdfdocument.cpp @@ -424,6 +424,8 @@ QRectF QPdfDocumentPrivate::getCharBox(FPDF_TEXTPAGE textPage, double pageHeight QPdfDocumentPrivate::TextPosition QPdfDocumentPrivate::hitTest(int page, QPointF position) { const QPdfMutexLocker lock; + + TextPosition result; FPDF_PAGE pdfPage = FPDF_LoadPage(doc, page); double pageHeight = FPDF_GetPageHeight(pdfPage); FPDF_TEXTPAGE textPage = FPDFText_LoadPage(pdfPage); @@ -440,10 +442,14 @@ QPdfDocumentPrivate::TextPosition QPdfDocumentPrivate::hitTest(int page, QPointF ++hitIndex; } qCDebug(qLcDoc) << "on page" << page << "@" << position << "got char position" << charPos << "index" << hitIndex; - return { charPos, charBox.height(), hitIndex }; + result = { charPos, charBox.height(), hitIndex }; } } - return {}; + + FPDFText_ClosePage(textPage); + FPDF_ClosePage(pdfPage); + + return result; } /*! @@ -789,6 +795,9 @@ QPdfSelection QPdfDocument::getSelection(int page, QPointF start, QPointF end) CharacterHitTolerance, CharacterHitTolerance); int endIndex = FPDFText_GetCharIndexAtPos(textPage, end.x(), pageHeight - end.y(), CharacterHitTolerance, CharacterHitTolerance); + + QPdfSelection result; + if (startIndex >= 0 && endIndex != startIndex) { if (startIndex > endIndex) qSwap(startIndex, endIndex); @@ -815,11 +824,15 @@ QPdfSelection QPdfDocument::getSelection(int page, QPointF start, QPointF end) bounds << QPolygonF(rect); } qCDebug(qLcDoc) << page << start << "->" << end << "found" << startIndex << "->" << endIndex << text; - return QPdfSelection(text, bounds, hull, startIndex, endIndex); + result = QPdfSelection(text, bounds, hull, startIndex, endIndex); + } else { + qCDebug(qLcDoc) << page << start << "->" << end << "nothing found"; } - qCDebug(qLcDoc) << page << start << "->" << end << "nothing found"; - return QPdfSelection(); + FPDFText_ClosePage(textPage); + FPDF_ClosePage(pdfPage); + + return result; } /*! @@ -860,6 +873,10 @@ QPdfSelection QPdfDocument::getSelectionAtIndex(int page, int startIndex, int ma hull = QRectF(d->getCharPosition(textPage, pageHeight, startIndex), QSizeF()); qCDebug(qLcDoc) << "on page" << page << "at index" << startIndex << "maxLength" << maxLength << "got" << text.length() << "chars," << rectCount << "rects within" << hull; + + FPDFText_ClosePage(textPage); + FPDF_ClosePage(pdfPage); + return QPdfSelection(text, bounds, hull, startIndex, startIndex + text.length()); } @@ -890,6 +907,10 @@ QPdfSelection QPdfDocument::getAllText(int page) bounds << QPolygonF(rect); } qCDebug(qLcDoc) << "on page" << page << "got" << count << "chars," << rectCount << "rects within" << hull; + + FPDFText_ClosePage(textPage); + FPDF_ClosePage(pdfPage); + return QPdfSelection(text, bounds, hull, 0, count); } |