Skip to content

Commit

Permalink
Merge pull request #11 from TobiasBuchholz/fix/native_memory_leak_whe…
Browse files Browse the repository at this point in the history
…n_opening_pdf_from_byte_array

Fixed memory leak when opening PDF as a byte array
  • Loading branch information
johngray1965 authored Oct 31, 2023
2 parents d3da7b9 + 1b2a30f commit 016a4f4
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions PdfiumAndroid/src/main/cpp/pdfiumandroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class DocumentFile {
public:
FPDF_DOCUMENT pdfDocument = nullptr;

public:
jbyte *cDataCopy = NULL;

DocumentFile() { initLibraryIfNeed(); }
~DocumentFile();
};
Expand All @@ -69,7 +72,10 @@ DocumentFile::~DocumentFile(){
FPDF_CloseDocument(pdfDocument);
pdfDocument = nullptr;
}

if(cDataCopy != NULL){
free(cDataCopy);
cDataCopy = NULL;
}
destroyLibraryIfNeed();
}

Expand Down Expand Up @@ -277,13 +283,11 @@ Java_io_legere_pdfiumandroid_PdfiumCore_nativeOpenMemDocument(JNIEnv *env, jobje
cpassword = env->GetStringUTFChars(password, nullptr);
}

jbyte *cData = env->GetByteArrayElements(data, nullptr);
int size = (int) env->GetArrayLength(data);
auto *cDataCopy = new jbyte[size];
memcpy(cDataCopy, cData, size);
env->GetByteArrayRegion(data, 0, size, cDataCopy);
FPDF_DOCUMENT document = FPDF_LoadMemDocument( reinterpret_cast<const void*>(cDataCopy),
size, cpassword);
env->ReleaseByteArrayElements(data, cData, JNI_ABORT);

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

docFile->pdfDocument = document;

docFile->cDataCopy = cDataCopy;
return reinterpret_cast<jlong>(docFile);
}

Expand Down

0 comments on commit 016a4f4

Please sign in to comment.