Skip to content

Commit

Permalink
Fixed memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Balearica committed Oct 31, 2024
1 parent 92ec67b commit 20d39c8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
3 changes: 3 additions & 0 deletions js/containers/imageContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ export class ImageCache {

const workersPromiseArr = range(0, scheduler.workers.length - 1).map(async (x) => {
const w = scheduler.workers[x];

if (w.pdfDoc) await w.freeDocument(w.pdfDoc);

// The ArrayBuffer is transferred to the worker, so a new one must be created for each worker.
// const fileData = await file.arrayBuffer();
const fileDataCopy = fileData.slice(0);
Expand Down
4 changes: 4 additions & 0 deletions js/export/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ export async function exportData(format = 'txt', minValue = 0, maxValue = -1) {
doc1: pdfOverlay, minpage: minValue, maxpage: maxValue, pagewidth: dimsLimit.width, pageheight: dimsLimit.height, humanReadable: opt.humanReadablePDF,
});
}

w.freeDocument(pdfOverlay);
} else {
const pdfStr = await writePdf(ocrDownload, minValue, maxValue, opt.displayMode, false, true, dimsLimit, opt.confThreshHigh, opt.confThreshMed,
opt.overlayOpacity / 100);
Expand All @@ -169,6 +171,8 @@ export async function exportData(format = 'txt', minValue = 0, maxValue = -1) {
content = await w.write({
doc1: pdf, minpage: minValue, maxpage: maxValue, pagewidth: dimsLimit.width, pageheight: dimsLimit.height, humanReadable: opt.humanReadablePDF,
});

w.freeDocument(pdf);
}
} else if (format === 'hocr') {
content = writeHocr(ocrAll.active, minValue, maxValue);
Expand Down
2 changes: 1 addition & 1 deletion mupdf/mupdf-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export async function initMuPDFWorker() {
return function (...args) {
return new Promise((resolve, reject) => {
// Add the PDF as the first argument for most functions
if (!['openDocument', 'cleanFile'].includes(func)) {
if (!['openDocument', 'cleanFile', 'freeDocument'].includes(func)) {
// Remove job number (appended by Tesseract scheduler function)
// args = args.slice(0,-1)

Expand Down
4 changes: 3 additions & 1 deletion mupdf/mupdf-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ mupdf.pageText = function (doc, {

const content = Module.UTF8ToString(dataPtr);

Module._free(dataPtr);

return {
letterCountTotal,
letterCountVis,
Expand Down Expand Up @@ -464,7 +466,7 @@ const handleMessage = (data) => {
} catch (error) {
parentPort.postMessage(['ERROR', id, { name: error.name, message: error.message }]);
}
}
};

if (typeof process === 'undefined') {
onmessage = (event) => handleMessage(event.data);
Expand Down

0 comments on commit 20d39c8

Please sign in to comment.