Skip to content

Commit 8678ab3

Browse files
fix: memory leak
1 parent 9bb1e6b commit 8678ab3

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

lib/image/image.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const {RGB_IMAGE_CHANNELS, BITS_IN_BYTE} = require('../constants');
77

88
const supportedFormats = ['JPEG', 'PNG', 'WEBP', 'GIF', 'AVIF', 'TIFF', 'SVG'];
99

10+
// eslint-disable-next-line no-undef
11+
const imageMagickFinalizationRegistry = new FinalizationRegistry(fn => fn());
12+
1013
module.exports = class Image extends ImageBase {
1114
constructor(buffer, rgb) {
1215
super();
@@ -17,10 +20,12 @@ module.exports = class Image extends ImageBase {
1720
settings = new magick.MagickReadSettings({format: 'RGB', width: rgb.width, height: rgb.height});
1821
}
1922

20-
this._img = magick.MagickImage.create(buffer, settings);
23+
const imgRef = this._img = magick.MagickImage.create(buffer, settings);
2124

2225
this._width = this._img.width;
2326
this._height = this._img.height;
27+
28+
imageMagickFinalizationRegistry.register(this, () => imgRef.dispose && imgRef.dispose());
2429
}
2530

2631
/**

0 commit comments

Comments
 (0)