Is it correct that this code causes a memory leak in the browser?
function loadImageDrawIntoCanvas(ctx, x, y, url) {
var img = new Image();
img.onload = function() {
ctx.drawImage(img, x, y);
}
img.src = url;
};
I understand that since img is a DOM element, and because I attach JavaScript to it with the help img.onload, the browser will never garbage collect this. To fix this, I need to clean img.onload, as in
function loadImageDrawIntoCanvas(ctx, x, y, url) {
var img = new Image();
img.onload = function() {
ctx.drawImage(img, x, y);
img.onload = null;
img = null;
}
img.src = url;
};
source
share