webassembly
webassembly copied to clipboard
WebP: converting many images - out of bounds errors or memory leaks
If I try to share a module instance for multiple image conversions, I end up getting out of bounds errors and nothing works, and if I make a new module instance for every image then there are no issues, but there are very big memory leaks that kill the process with enough conversions. I am using Node if it matters.
// Sharing the same instance
let webpModule: WebPModule | null = null;
async function encodeWebP(image: Jimp): Promise<Buffer> {
if (!webpModule) {
webpModule = await wasm_webp();
}
// RuntimeError: memory access out of bounds
const result = <Uint8Array>webpModule.encode(image.bitmap.data.buffer, image.getWidth(), image.getHeight(), 4, defaultOptions);
webpModule.free();
return Buffer.from(result);
}
// New instance per image
async function encodeWebP(image: Jimp): Promise<Buffer> {
const webpModule = await wasm_webp();
// Works, but leaks memory
const result = <Uint8Array>webpModule.encode(image.bitmap.data.buffer, image.getWidth(), image.getHeight(), 4, defaultOptions);
webpModule.free();
return Buffer.from(result);
}
If it's any help, when a new instance is created per image I eventually also get Node maximum listener warnings.
I wonder if after running encodeWebP, keeping the Buffer.from result is keeping webpModule in memory. Try assigning webpModule to null in the second example after webpModule.free().