Skip to content
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.barteksc.pdfviewer;

// Declared in AndroidPdfViewer's package to reach PDFView's package-private
// pdfFile/renderingHandler fields and the package-private PdfFile class.
//
// PDFView.recycle() disposes the PdfFile synchronously on the main thread while
// RenderingHandler may still be rendering a page on its own thread, which closes
// the native pdfium document under the renderer (SIGSEGV in libpdfium.so, see
// https://github.com/wonday/react-native-pdf/issues/1024 and
// https://github.com/wonday/react-native-pdf/issues/1041). Posting the dispose on
// the rendering handler serializes it after any in-flight render.
public final class PdfFileDisposer {
private PdfFileDisposer() {}

// Detaches the PdfFile so that PDFView.recycle() skips its synchronous dispose,
// and returns a task disposing it on the rendering thread. Must be called right
// before super.recycle(), and the returned task run right after it.
public static Runnable detach(PDFView view) {
final PdfFile pdfFile = view.pdfFile;
final RenderingHandler renderingHandler = view.renderingHandler;
// RenderingHandler.proceed() reads view.pdfFile when it dequeues a task, so drop
// the queued tasks (as recycle() does) before clearing the field.
if (renderingHandler != null) {
renderingHandler.stop();
renderingHandler.removeMessages(RenderingHandler.MSG_RENDER_TASK);
}
view.pdfFile = null;
if (pdfFile == null) {
return () -> {};
}
return () -> {
// post() fails once the rendering thread has quit: nothing can be
// rendering anymore, so disposing on the current thread is safe.
if (renderingHandler == null || !renderingHandler.post(pdfFile::dispose)) {
pdfFile.dispose();
}
};
}
}
9 changes: 9 additions & 0 deletions android/src/main/java/org/wonday/pdf/PdfView.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIManagerHelper;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.PdfFileDisposer;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnErrorListener;
Expand Down Expand Up @@ -329,6 +330,14 @@ protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}

// Defers the native document dispose to the rendering thread, see PdfFileDisposer.
@Override
public void recycle() {
Runnable disposePdfFile = PdfFileDisposer.detach(this);
super.recycle();
disposePdfFile.run();
}

private int getPdfPageCount(File pdfFile) throws IOException {
ParcelFileDescriptor fileDescriptor =
ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_ONLY);
Expand Down