Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed memory leak when opening PDF as a byte array #11

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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