Skip to content

Commit 6fb20cb

Browse files
fix: memory leak
1 parent 9bb1e6b commit 6fb20cb

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

lib/image/image.js

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

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

10+
const imageMagickFinalizationRegistry = new FinalizationRegistry(fn => fn())
11+
1012
module.exports = class Image extends ImageBase {
1113
constructor(buffer, rgb) {
1214
super();
@@ -17,10 +19,12 @@ module.exports = class Image extends ImageBase {
1719
settings = new magick.MagickReadSettings({format: 'RGB', width: rgb.width, height: rgb.height});
1820
}
1921

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

2224
this._width = this._img.width;
2325
this._height = this._img.height;
26+
27+
imageMagickFinalizationRegistry.register(this, () => imgRef.dispose());
2428
}
2529

2630
/**

0 commit comments

Comments
 (0)