add some logs, fix frontend memory leak with users not deallocating their haku

closes #28
This commit is contained in:
liquidex 2024-08-22 20:49:24 +02:00
parent 6c88a041ea
commit 4ebd150e59
3 changed files with 28 additions and 5 deletions

View file

@ -72,11 +72,14 @@ impl Default for Limits {
#[no_mangle] #[no_mangle]
extern "C" fn haku_limits_new() -> *mut Limits { 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] #[no_mangle]
unsafe extern "C" fn haku_limits_destroy(limits: *mut Limits) { unsafe extern "C" fn haku_limits_destroy(limits: *mut Limits) {
debug!("destroying limits: {limits:?}");
drop(Box::from_raw(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, value: Value::Nil,
exception: None, exception: None,
}); });
Box::leak(instance)
let ptr = Box::leak(instance) as *mut _;
debug!("created instance: {ptr:?}");
ptr
} }
#[no_mangle] #[no_mangle]
unsafe extern "C" fn haku_instance_destroy(instance: *mut Instance) { unsafe extern "C" fn haku_instance_destroy(instance: *mut Instance) {
debug!("destroying instance: {instance:?}");
drop(Box::from_raw(instance)); drop(Box::from_raw(instance));
} }
@ -253,11 +260,14 @@ struct Brush {
#[no_mangle] #[no_mangle]
extern "C" fn haku_brush_new() -> *mut Brush { 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] #[no_mangle]
unsafe extern "C" fn haku_brush_destroy(brush: *mut Brush) { unsafe extern "C" fn haku_brush_destroy(brush: *mut Brush) {
debug!("destroying brush: {brush:?}");
drop(Box::from_raw(brush)) drop(Box::from_raw(brush))
} }
@ -350,13 +360,16 @@ struct PixmapLock {
#[no_mangle] #[no_mangle]
extern "C" fn haku_pixmap_new(width: u32, height: u32) -> *mut PixmapLock { 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"), pixmap: Pixmap::new(width, height).expect("invalid pixmap size"),
})) })) as *mut _;
debug!("created pixmap with size {width}x{height}: {ptr:?}");
ptr
} }
#[no_mangle] #[no_mangle]
unsafe extern "C" fn haku_pixmap_destroy(pixmap: *mut PixmapLock) { unsafe extern "C" fn haku_pixmap_destroy(pixmap: *mut PixmapLock) {
debug!("destroying pixmap: {pixmap:?}");
drop(Box::from_raw(pixmap)) drop(Box::from_raw(pixmap))
} }

View file

@ -130,6 +130,11 @@ export class Haku {
w.haku_limits_destroy(pLimits); w.haku_limits_destroy(pLimits);
} }
destroy() {
w.haku_brush_destroy(this.#pBrush);
w.haku_instance_destroy(this.#pInstance);
}
setBrush(code) { setBrush(code) {
w.haku_reset(this.#pInstance); w.haku_reset(this.#pInstance);
// NOTE: Brush is invalid at this point, because we reset removes all defs and registered chunks. // NOTE: Brush is invalid at this point, because we reset removes all defs and registered chunks.

View file

@ -15,6 +15,10 @@ export class User {
this.painter = new Painter(wallInfo.paintArea); this.painter = new Painter(wallInfo.paintArea);
} }
destroy() {
this.haku.destroy();
}
setBrush(brush) { setBrush(brush) {
let compileResult = this.haku.setBrush(brush); let compileResult = this.haku.setBrush(brush);
this.isBrushOk = compileResult.status == "ok"; this.isBrushOk = compileResult.status == "ok";
@ -57,6 +61,7 @@ export class OnlineUsers extends EventTarget {
removeUser(sessionId) { removeUser(sessionId) {
if (this.#users.has(sessionId)) { if (this.#users.has(sessionId)) {
let user = this.#users.get(sessionId); let user = this.#users.get(sessionId);
user.destroy();
console.info("user removed", sessionId, user.nickname); console.info("user removed", sessionId, user.nickname);
// TODO: Cleanup reticles // TODO: Cleanup reticles
this.#users.delete(sessionId); this.#users.delete(sessionId);