From 402d6ffa2f476604ee40f54e28f13fe2eccb4afd Mon Sep 17 00:00:00 2001 From: "Juergen D. Geltinger" Date: Sun, 30 Dec 2018 15:20:49 +0100 Subject: [PATCH] Fixed v8 memory leak Local v8 DefaultPlatform initialization was not released when wxPDFViewImpl got destroyed --- include/private/PDFViewImpl.h | 5 ++++- src/PDFViewBookmarks.cpp | 4 ++-- src/PDFViewImpl.cpp | 20 +++++++++++++------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/include/private/PDFViewImpl.h b/include/private/PDFViewImpl.h index 03e896e..cfb3f98 100644 --- a/include/private/PDFViewImpl.h +++ b/include/private/PDFViewImpl.h @@ -27,6 +27,9 @@ #include "PDFViewBookmarks.h" #include "PDFViewTextRange.h" +#include "v8.h" +#include "libplatform/libplatform.h" + class wxPDFViewImpl: public wxPDFViewPagesClient, public wxEvtHandler { public: @@ -133,7 +136,7 @@ class wxPDFViewImpl: public wxPDFViewPagesClient, public wxEvtHandler // PDF SDK Structures static wxAtomicInt ms_pdfSDKRefCount; - static bool ms_v8initialized; + static v8::Platform* ms_platform; FPDF_FILEACCESS m_pdfFileAccess; FX_FILEAVAIL m_pdfFileAvail; FX_DOWNLOADHINTS m_hints; diff --git a/src/PDFViewBookmarks.cpp b/src/PDFViewBookmarks.cpp index ec77315..1ff0e07 100644 --- a/src/PDFViewBookmarks.cpp +++ b/src/PDFViewBookmarks.cpp @@ -41,7 +41,7 @@ class wxPDFViewBookmarkImpl: public wxPDFViewBookmark virtual void Navigate(wxPDFView* pdfView) { - FPDF_DOCUMENT doc = pdfView->GetImpl()->GetDocument(); + FPDF_DOCUMENT doc = static_cast(pdfView->GetImpl()->GetDocument()); FPDF_ACTION action = FPDFBookmark_GetAction(m_bookmark); FPDF_DEST dest = NULL; if (action) @@ -55,7 +55,7 @@ class wxPDFViewBookmarkImpl: public wxPDFViewBookmark } if (dest) { - unsigned long pageIndex = FPDFDest_GetPageIndex(doc, dest); + unsigned long pageIndex = FPDFDest_GetDestPageIndex(doc, dest); pdfView->GoToPage(pageIndex); } } diff --git a/src/PDFViewImpl.cpp b/src/PDFViewImpl.cpp index eb546dd..82eb9e6 100644 --- a/src/PDFViewImpl.cpp +++ b/src/PDFViewImpl.cpp @@ -385,7 +385,7 @@ class wxPDFViewActivity // wxAtomicInt wxPDFViewImpl::ms_pdfSDKRefCount = 0; -bool wxPDFViewImpl::ms_v8initialized = false; +v8::Platform* wxPDFViewImpl::ms_platform = 0; wxPDFViewImpl::wxPDFViewImpl(wxPDFView* ctrl): m_ctrl(ctrl), @@ -1317,7 +1317,7 @@ bool wxPDFViewImpl::EvaluateLinkTargetPageAtClientPos(const wxPoint& clientPos, } if (dest) - GoToPage(FPDFDest_GetPageIndex(m_pdfDoc, dest)); + GoToPage(FPDFDest_GetDestPageIndex(m_pdfDoc, dest)); } } else @@ -1571,7 +1571,7 @@ bool wxPDFViewImpl::AcquireSDK() if (ms_pdfSDKRefCount == 0) { // Initialize PDF Rendering library - if (!ms_v8initialized) + if (!ms_platform) { v8::V8::InitializeICU(); @@ -1583,16 +1583,14 @@ bool wxPDFViewImpl::AcquireSDK() wxString resPathStr = resPath.GetFullPath(); v8::V8::InitializeExternalStartupData(resPathStr.c_str()); - v8::Platform* platform = v8::platform::CreateDefaultPlatform(); - v8::V8::InitializePlatform(platform); + ms_platform = v8::platform::CreateDefaultPlatform(); + v8::V8::InitializePlatform(ms_platform); v8::V8::Initialize(); // By enabling predictable mode, V8 won't post any background tasks. const char predictable_flag[] = "--predictable"; v8::V8::SetFlagsFromString(predictable_flag, static_cast(strlen(predictable_flag))); - - ms_v8initialized = true; } FPDF_LIBRARY_CONFIG config; @@ -1613,6 +1611,14 @@ void wxPDFViewImpl::ReleaseSDK() wxAtomicDec(ms_pdfSDKRefCount); if (ms_pdfSDKRefCount == 0) { + if (ms_platform) + { + v8::V8::Dispose(); + v8::V8::ShutdownPlatform(); + delete ms_platform; + ms_platform = 0; + } + FPDF_DestroyLibrary(); } }