From ecf3a638e2c01e7f1d3694c1c43d9d89955f14da Mon Sep 17 00:00:00 2001 From: jiangmin Date: Wed, 6 Jan 2016 10:40:12 +0800 Subject: [PATCH] Wrap reference count to one module The reference count should be not managed by two module (PdfRenderer and PdfEditor) separately. It will cause memory leak and crash. FPDF_InitLibrary will be call twice and excute "g_FPDFAPI_pDefaultMgr = new CPDF_ModuleMgr;" twice. Memory leak will happen. PdfEditor will call FPDF_DestroyLibrary just after PdfRenderer calls FPDF_InitLibrary. PdfRenderer will execute FPDF's API and crash for null point of CPDF_ModuleMgr::Get() due to PdfEditor's calling for FPDF_DestroyLibrary. So we merge the reference count of FPDF library to PdfLibrary.cpp from PdfEditor.cpp and PdfRenderer.cpp. Change-Id: I984a268ddd2d6d97e086e51d6459751feeaf372a Signed-off-by: Wu Jiangming Tracked-On: https://jira01.devtools.intel.com/browse/OAM-15089 Reviewed-on: https://android.intel.com:443/455409 --- core/jni/Android.mk | 1 + core/jni/android/graphics/pdf/PdfEditor.cpp | 20 +-------- core/jni/android/graphics/pdf/PdfLibrary.cpp | 59 +++++++++++++++++++++++++++ core/jni/android/graphics/pdf/PdfRenderer.cpp | 20 +-------- 4 files changed, 64 insertions(+), 36 deletions(-) create mode 100644 core/jni/android/graphics/pdf/PdfLibrary.cpp diff --git a/core/jni/Android.mk b/core/jni/Android.mk index 86c1918295cb..dbc0ed53f396 100644 --- a/core/jni/Android.mk +++ b/core/jni/Android.mk @@ -139,6 +139,7 @@ LOCAL_SRC_FILES:= \ android/graphics/Xfermode.cpp \ android/graphics/YuvToJpegEncoder.cpp \ android/graphics/pdf/PdfDocument.cpp \ + android/graphics/pdf/PdfLibrary.cpp \ android/graphics/pdf/PdfEditor.cpp \ android/graphics/pdf/PdfRenderer.cpp \ android_media_AudioRecord.cpp \ diff --git a/core/jni/android/graphics/pdf/PdfEditor.cpp b/core/jni/android/graphics/pdf/PdfEditor.cpp index 52b69e02e894..cfe715521c14 100644 --- a/core/jni/android/graphics/pdf/PdfEditor.cpp +++ b/core/jni/android/graphics/pdf/PdfEditor.cpp @@ -51,25 +51,9 @@ static struct { jfieldID bottom; } gRectClassInfo; -static Mutex sLock; +extern void initializeLibraryIfNeeded(); -static int sUnmatchedInitRequestCount = 0; - -static void initializeLibraryIfNeeded() { - Mutex::Autolock _l(sLock); - if (sUnmatchedInitRequestCount == 0) { - FPDF_InitLibrary(); - } - sUnmatchedInitRequestCount++; -} - -static void destroyLibraryIfNeeded() { - Mutex::Autolock _l(sLock); - sUnmatchedInitRequestCount--; - if (sUnmatchedInitRequestCount == 0) { - FPDF_DestroyLibrary(); - } -} +extern void destroyLibraryIfNeeded(); static int getBlock(void* param, unsigned long position, unsigned char* outBuffer, unsigned long size) { diff --git a/core/jni/android/graphics/pdf/PdfLibrary.cpp b/core/jni/android/graphics/pdf/PdfLibrary.cpp new file mode 100644 index 000000000000..2f94e0057822 --- /dev/null +++ b/core/jni/android/graphics/pdf/PdfLibrary.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2015 Intel iCDG + * + * Move the reference count of fpdfview in PdfRenderer.cpp and PdfEditor.cpp + * to PdfLibrary.cpp + * + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "jni.h" +#include "JNIHelp.h" +#include "fpdfview.h" + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor" +#include "fsdk_rendercontext.h" +#pragma GCC diagnostic pop + +#include "core_jni_helpers.h" +#include +#include +#include +#include + +namespace android { + +static Mutex sLock; + +static int sUnmatchedInitRequestCount = 0; + +void initializeLibraryIfNeeded() { + Mutex::Autolock _l(sLock); + if (sUnmatchedInitRequestCount == 0) { + FPDF_InitLibrary(); + } + sUnmatchedInitRequestCount++; +} + +void destroyLibraryIfNeeded() { + Mutex::Autolock _l(sLock); + sUnmatchedInitRequestCount--; + if (sUnmatchedInitRequestCount == 0) { + FPDF_DestroyLibrary(); + } +} + +}; diff --git a/core/jni/android/graphics/pdf/PdfRenderer.cpp b/core/jni/android/graphics/pdf/PdfRenderer.cpp index 006eef847a12..a23cc58af4fd 100644 --- a/core/jni/android/graphics/pdf/PdfRenderer.cpp +++ b/core/jni/android/graphics/pdf/PdfRenderer.cpp @@ -43,25 +43,9 @@ static struct { jfieldID y; } gPointClassInfo; -static Mutex sLock; +extern void initializeLibraryIfNeeded(); -static int sUnmatchedInitRequestCount = 0; - -static void initializeLibraryIfNeeded() { - Mutex::Autolock _l(sLock); - if (sUnmatchedInitRequestCount == 0) { - FPDF_InitLibrary(); - } - sUnmatchedInitRequestCount++; -} - -static void destroyLibraryIfNeeded() { - Mutex::Autolock _l(sLock); - sUnmatchedInitRequestCount--; - if (sUnmatchedInitRequestCount == 0) { - FPDF_DestroyLibrary(); - } -} +extern void destroyLibraryIfNeeded(); static int getBlock(void* param, unsigned long position, unsigned char* outBuffer, unsigned long size) { -- 2.11.0