Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebP: converting many images - out of bounds errors or memory leaks #658

Open
flowtsohg opened this issue Nov 16, 2022 · 1 comment
Open

Comments

@flowtsohg
Copy link

flowtsohg commented Nov 16, 2022

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.

@jonshipmansmc
Copy link

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().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants