We are developing multi-threaded java application using licensed Leadtools SDK 20 for converting large pdf documents to searchable pdf documents. During OCR process Java Virtual Machine randomly crashes - EXCEPTION_ACCESS_VIOLATION(Problematic frame: C [Ltocrx.dll+0x2af3a]).
I've noticed that it may happens if user try to cancel OCR process.
public class OcrProgressCallback implements OcrProgressListener {
@Override
public void onProgress(OcrProgressData ocrProgressData) {
if (ocrProgressData.getStatus() != OcrProgressStatus.ABORT) {
ocrDocument.ifPresent(doc -> {
ProcessingStatus progressStatus = doc.getStatus();
if (progressStatus == ProcessingStatus.CANCELED) {
ocrProgressData.setStatus(OcrProgressStatus.ABORT);
throw new RasterException(RasterExceptionCode.USER_ABORT);
}
});
}
}
}
Bellow attached log file:
# EXCEPTION_ACCESS_VIOLATION
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [Ltocrx.dll+0x2af3a]
C [Ltocrx.dll+0x13d94]
C 0x00000000020c8c67
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j leadtools.ocr.Ltocr.OcrPageDestroy(J)I+0
j leadtools.ocr.OcrPage.dispose()V+52
j leadtools.ocr.OcrPage.finalize()V+1
J 2364% C2 java.lang.ref.Finalizer$FinalizerThread.run()V (55 bytes) @ 0x0000000002738ac4 [0x0000000002738820+0x2a4]
v ~StubRoutines::call_stub
OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD);
ocrEngine.startup(null, null, null, ocrRuntimePath);
setInitialOcrConfiguration(processingDocument, ocrEngine);
RasterCodecs rasterCodecs = ocrEngine.getRasterCodecsInstance();
rasterCodecs.startOptimizedLoad();
OcrDocument ocrDocument = ocrEngine.getDocumentManager()
.createDocument(null, OcrCreateDocumentOptions.AUTO_DELETE_FILE.getValue());
OcrProgressCallback ocrProgressCallback = new OcrProgressCallback(processingDocumentRepository, processingDocument);
Path inputPath = ....
Path outputPath = generateOutputDocumentPath();
try {
int pageCount = rasterCodecs.getTotalPages(inputPath.toString());
IntStream.rangeClosed(1, pageCount)
.forEach(pageNumber -> {
RasterImage rasterImage = rasterCodecs.load(inputPath.toString(), pageNumber);
OcrPage ocrPage = ocrEngine.createPage(rasterImage, OcrImageSharingMode.AUTO_DISPOSE);
ocrPage.autoZone(null);
ocrPage.recognize(ocrProgressCallback);
ocrDocument.getPages().add(ocrPage);
rasterImage.dispose();
ocrPage.dispose();
});
ocrDocument.save(outputPath, DocumentFormat.PDF, null);
} finally {
if(Objects.nonNull(rasterCodecs)) {
rasterCodecs.detachOptimizedLoadData();
rasterCodecs.stopOptimizedLoad();
}
if (Objects.nonNull(rasterCodecs)) {
rasterCodecs.dispose();
}
if (Objects.nonNull(ocrDocument)) {
ocrDocument.dispose();
}
ocrEngine.shutdown();
}