I discovered this bug when running the Memlab tool over an app that adds and removes Dynamic Textures. It turns out the the _canvas element can sometimes cause memory to fail to be garbage collected. This snippet in dynamicTexture.ts appears to fix it.
/**
* Disposes the dynamic texture. The DOM elements should be removed before the texture is disposed
*/
public dispose(): void {
if(this._canvas) {
this._canvas.remove();
(this._canvas as any) = null;
}
if(this._context) {
(this._context as any) = null;
}
super.dispose();
}
And then in ICanvas, add:
/**
* Removes the canvas from the document.
*/
remove(): void;
I am also to wrap the dispose function in my own code but it seems like the BabylonJS class is the better place for it. Apologies, but I canāt submit a pull request.
Iām not sure why the canvas is not automatically garbage collected by the browser, if the dynamic texture is not referenced by any object anymore. Maybe the texture was still referenced somewhereā¦
Anyway, it does no harm to add a dispose method to DynamicTexture, so I created a PR with your code (with a small change, because we should not do anything with the canvas if we didnāt create it in the first place):
I looked through our code and couldnāt find anywhere weāre disposing multiple times, but I was able to do a simple PG that repros the issue that way so it sounds like a plausible explanation. Thank you for the update. I also pushed a PR but Iāll close mine since you did the same thing already.