emscripten-core / emscripten-core/emscripten
getContextSafariWebGL2Fixed create memory leak ?
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
When running emcc with flag `-s MODULARIZE=1 -s environment=web`, it will generate a commonJS module.
Inside the module function, it stores the wasmMemory and other staff, and it replaces the original `canvas.getContext` with getContextSafariWebGL2Fixed
```
// BUG: Workaround Safari WebGL issue: After successfully acquiring WebGL context on a canvas,
// calling .getContext() will always return that context independent of which 'webgl' or 'webgl2'
// context version was passed. See https://bugs.webkit.org/show_bug.cgi?id=222758 and
// https://github.com/emscripten-core/emscripten/issues/13295.
// TODO: Once the bug is fixed and shipped in Safari, adjust the Safari version field in above check.
if (!canvas.getContextSafariWebGL2Fixed) {
canvas.getContextSafariWebGL2Fixed = canvas.getContext;
/** @type {function(this:HTMLCanvasElement, string, (Object|null)=): (Object|null)} */
function fixedGetContext(ver, attrs) {
var gl = canvas.getContextSafariWebGL2Fixed(ver, attrs);
return ((ver == 'webgl') == (gl instanceof WebGLRenderingContext)) ? gl : null;
}
canvas.getContext = fixedGetContext;
}
```
Usually this is not a problem, however, I want to "reload" the module, in order to GC the whole wasmMemory buffer.
For example:
```
delete require.cache[require.resolve('./mywasm.js')];
var module = require('./mywasm.js')
```
However, I found out that as long as this function is kept by the canvas element, and the canvas element is kept by the DOM tree, or by some JavaScript (e.g. FiberNode on React), the memory will be kept alive indirectly
Possible solution I found:
1. Remove the canvas from the DOM tree directly, and make sure no reference to the unattached DOM as well (which is not MVVM framework like React and Angular is designed to do ...)
2. Delete this monkey patch function manually
Or is there a flag so that this function will not be generated because I don't need to support Safari
Contributor guide
Assessment
This issue has not been assessed yet.