Avoid possible leaks when loading html images and then destroying the window

Keep weak references to the canvas, etc. for the DOM closure
This commit is contained in:
Simon Hausmann 2021-01-11 20:15:55 +01:00
parent 0b20d13401
commit 30bc16db73

View file

@ -367,12 +367,22 @@ impl GLItemRenderer {
html_image.set_cross_origin(Some("anonymous")); html_image.set_cross_origin(Some("anonymous"));
html_image.set_onload(Some( html_image.set_onload(Some(
&wasm_bindgen::closure::Closure::once_into_js({ &wasm_bindgen::closure::Closure::once_into_js({
let canvas = self.canvas.clone(); let canvas_weak = Rc::downgrade(&self.canvas);
let html_image = html_image.clone(); let html_image = html_image.clone();
let image_id = image_id.clone(); let image_id = image_id.clone();
let window = self.window.clone(); let window_weak = Rc::downgrade(&self.window);
let event_loop_proxy = self.event_loop_proxy.clone(); let event_loop_proxy_weak = Rc::downgrade(&self.event_loop_proxy);
move || { move || {
let (canvas, window, event_loop_proxy) = match (
canvas_weak.upgrade(),
window_weak.upgrade(),
event_loop_proxy_weak.upgrade(),
) {
(Some(canvas), Some(window), Some(event_loop_proxy)) => {
(canvas, window, event_loop_proxy)
}
_ => return,
};
canvas canvas
.borrow_mut() .borrow_mut()
.realloc_image( .realloc_image(