Skip to content

Commit

Permalink
More explicitly try to release canvas
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Sep 5, 2024
1 parent 2cdf87d commit 5c74273
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/vendored/chartjs-node-canvas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export class ChartJSNodeCanvas {
}
const canvas = chart.canvas as Canvas;
canvas.toDataURL(mimeType, (error: Error | null, png: string) => {
chart.platform.releaseContext(chart.ctx);
(<any>chart).canvas = null;
(<any>chart).ctx = null;

chart.destroy();
if (error) {
return reject(error);
Expand All @@ -137,6 +141,10 @@ export class ChartJSNodeCanvas {
}
const canvas = chart.canvas as Canvas;
const dataUrl = canvas.toDataURL(mimeType);
chart.platform.releaseContext(chart.ctx);
(<any>chart).canvas = null;
(<any>chart).ctx = null;

chart.destroy();
return dataUrl;
}
Expand Down Expand Up @@ -164,6 +172,10 @@ export class ChartJSNodeCanvas {
}
const canvas = chart.canvas as Canvas;
canvas.toBuffer((error: Error | null, buffer: Buffer) => {
chart.platform.releaseContext(chart.ctx);
(<any>chart).canvas = null;
(<any>chart).ctx = null;

chart.destroy();
if (error) {
return reject(error);
Expand Down Expand Up @@ -196,6 +208,11 @@ export class ChartJSNodeCanvas {
}
const canvas = chart.canvas as Canvas;
const buffer = canvas.toBuffer(mimeType);

chart.platform.releaseContext(chart.ctx);
(<any>chart).canvas = null;
(<any>chart).ctx = null;

chart.destroy();
return buffer;
}
Expand All @@ -220,7 +237,12 @@ export class ChartJSNodeCanvas {
throw new Error('Canvas is null');
}
const canvas = chart.canvas as Canvas;
setImmediate(() => chart.destroy());
setImmediate(() => {
chart.platform.releaseContext(chart.ctx);
(<any>chart).canvas = null;
(<any>chart).ctx = null;
chart.destroy();
});
switch (mimeType) {
case 'image/png':
return canvas.createPNGStream();
Expand Down

0 comments on commit 5c74273

Please sign in to comment.