Skip to content

Commit cf47d5f

Browse files
Add workaround for issue #4. Don't crash program on multiple conversion calls.
1 parent 2ada468 commit cf47d5f

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

pdf2htmlEX/src/main/cpp/pdf2htmlEX.cc

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <string>
2424
#include <vector>
2525
#include <jni.h>
26+
#include <unistd.h>
27+
#include <sys/wait.h>
2628
#include "pdf2htmlEX.h"
2729

2830
// Creating char ** by hand is rather annoying.
@@ -92,10 +94,26 @@ Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_call_1pdf2htmlEX(JNIEnv *e
9294

9395
int argc;
9496
char **argv;
95-
int retVal;
97+
int retVal = -1;
98+
9699
vector_to_char_pp(args, &argc, &argv);
97100

98-
retVal = pdf2htmlEX_main(argc, argv);
101+
// https://github.com/ViliusSutkus89/pdf2htmlEX-Android/issues/4
102+
// Upstream library is actually an executable, not a library.
103+
// May have some global state, initialized as static constructor, poisoned after first use.
104+
// Workaround: fork process before use.
105+
pid_t pid = fork();
106+
if (0 == pid) {
107+
retVal = pdf2htmlEX_main(argc, argv);
108+
exit(retVal);
109+
}
110+
else if (0 < pid) {
111+
int wstatus;
112+
waitpid(pid, &wstatus, 0);
113+
if (WIFEXITED(wstatus)) {
114+
retVal = WEXITSTATUS(wstatus);
115+
}
116+
}
99117

100118
for (int i = 0; i < argc; i++) {
101119
delete[] argv[i];

0 commit comments

Comments
 (0)