Skip to content

Commit 016a4f4

Browse files
authored
Merge pull request #11 from TobiasBuchholz/fix/native_memory_leak_when_opening_pdf_from_byte_array
Fixed memory leak when opening PDF as a byte array
2 parents d3da7b9 + 1b2a30f commit 016a4f4

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

PdfiumAndroid/src/main/cpp/pdfiumandroid.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class DocumentFile {
6060
public:
6161
FPDF_DOCUMENT pdfDocument = nullptr;
6262

63+
public:
64+
jbyte *cDataCopy = NULL;
65+
6366
DocumentFile() { initLibraryIfNeed(); }
6467
~DocumentFile();
6568
};
@@ -69,7 +72,10 @@ DocumentFile::~DocumentFile(){
6972
FPDF_CloseDocument(pdfDocument);
7073
pdfDocument = nullptr;
7174
}
72-
75+
if(cDataCopy != NULL){
76+
free(cDataCopy);
77+
cDataCopy = NULL;
78+
}
7379
destroyLibraryIfNeed();
7480
}
7581

@@ -277,13 +283,11 @@ Java_io_legere_pdfiumandroid_PdfiumCore_nativeOpenMemDocument(JNIEnv *env, jobje
277283
cpassword = env->GetStringUTFChars(password, nullptr);
278284
}
279285

280-
jbyte *cData = env->GetByteArrayElements(data, nullptr);
281286
int size = (int) env->GetArrayLength(data);
282287
auto *cDataCopy = new jbyte[size];
283-
memcpy(cDataCopy, cData, size);
288+
env->GetByteArrayRegion(data, 0, size, cDataCopy);
284289
FPDF_DOCUMENT document = FPDF_LoadMemDocument( reinterpret_cast<const void*>(cDataCopy),
285290
size, cpassword);
286-
env->ReleaseByteArrayElements(data, cData, JNI_ABORT);
287291

288292
if(cpassword != nullptr) {
289293
env->ReleaseStringUTFChars(password, cpassword);
@@ -308,7 +312,7 @@ Java_io_legere_pdfiumandroid_PdfiumCore_nativeOpenMemDocument(JNIEnv *env, jobje
308312
}
309313

310314
docFile->pdfDocument = document;
311-
315+
docFile->cDataCopy = cDataCopy;
312316
return reinterpret_cast<jlong>(docFile);
313317
}
314318

0 commit comments

Comments
 (0)