From 4ebd150e59f7653f809b5ed461c5358b3b8a082c Mon Sep 17 00:00:00 2001 From: liquidev Date: Thu, 22 Aug 2024 20:49:24 +0200 Subject: [PATCH] add some logs, fix frontend memory leak with users not deallocating their haku closes #28 --- crates/haku-wasm/src/lib.rs | 23 ++++++++++++++++++----- static/haku.js | 5 +++++ static/online-users.js | 5 +++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/crates/haku-wasm/src/lib.rs b/crates/haku-wasm/src/lib.rs index b510c29..5e273dc 100644 --- a/crates/haku-wasm/src/lib.rs +++ b/crates/haku-wasm/src/lib.rs @@ -72,11 +72,14 @@ impl Default for Limits { #[no_mangle] extern "C" fn haku_limits_new() -> *mut Limits { - Box::leak(Box::new(Limits::default())) + let ptr = Box::leak(Box::new(Limits::default())) as *mut _; + debug!("created limits: {ptr:?}"); + ptr } #[no_mangle] unsafe extern "C" fn haku_limits_destroy(limits: *mut Limits) { + debug!("destroying limits: {limits:?}"); drop(Box::from_raw(limits)) } @@ -156,11 +159,15 @@ unsafe extern "C" fn haku_instance_new(limits: *const Limits) -> *mut Instance { value: Value::Nil, exception: None, }); - Box::leak(instance) + + let ptr = Box::leak(instance) as *mut _; + debug!("created instance: {ptr:?}"); + ptr } #[no_mangle] unsafe extern "C" fn haku_instance_destroy(instance: *mut Instance) { + debug!("destroying instance: {instance:?}"); drop(Box::from_raw(instance)); } @@ -253,11 +260,14 @@ struct Brush { #[no_mangle] extern "C" fn haku_brush_new() -> *mut Brush { - Box::leak(Box::new(Brush::default())) + let ptr = Box::leak(Box::new(Brush::default())) as *mut _; + debug!("created brush: {ptr:?}"); + ptr } #[no_mangle] unsafe extern "C" fn haku_brush_destroy(brush: *mut Brush) { + debug!("destroying brush: {brush:?}"); drop(Box::from_raw(brush)) } @@ -350,13 +360,16 @@ struct PixmapLock { #[no_mangle] extern "C" fn haku_pixmap_new(width: u32, height: u32) -> *mut PixmapLock { - Box::leak(Box::new(PixmapLock { + let ptr = Box::leak(Box::new(PixmapLock { pixmap: Pixmap::new(width, height).expect("invalid pixmap size"), - })) + })) as *mut _; + debug!("created pixmap with size {width}x{height}: {ptr:?}"); + ptr } #[no_mangle] unsafe extern "C" fn haku_pixmap_destroy(pixmap: *mut PixmapLock) { + debug!("destroying pixmap: {pixmap:?}"); drop(Box::from_raw(pixmap)) } diff --git a/static/haku.js b/static/haku.js index b756302..f1a9775 100644 --- a/static/haku.js +++ b/static/haku.js @@ -130,6 +130,11 @@ export class Haku { w.haku_limits_destroy(pLimits); } + destroy() { + w.haku_brush_destroy(this.#pBrush); + w.haku_instance_destroy(this.#pInstance); + } + setBrush(code) { w.haku_reset(this.#pInstance); // NOTE: Brush is invalid at this point, because we reset removes all defs and registered chunks. diff --git a/static/online-users.js b/static/online-users.js index aef3fb4..de5b82d 100644 --- a/static/online-users.js +++ b/static/online-users.js @@ -15,6 +15,10 @@ export class User { this.painter = new Painter(wallInfo.paintArea); } + destroy() { + this.haku.destroy(); + } + setBrush(brush) { let compileResult = this.haku.setBrush(brush); this.isBrushOk = compileResult.status == "ok"; @@ -57,6 +61,7 @@ export class OnlineUsers extends EventTarget { removeUser(sessionId) { if (this.#users.has(sessionId)) { let user = this.#users.get(sessionId); + user.destroy(); console.info("user removed", sessionId, user.nickname); // TODO: Cleanup reticles this.#users.delete(sessionId);