From 06929963f2432b0aba49b3c313ec3638321d1a72 Mon Sep 17 00:00:00 2001 From: MaxOhn Date: Wed, 10 Jul 2024 10:16:41 +0200 Subject: [PATCH 1/2] add warning in readme to free Beatmap manually --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index d6d3a7e..960b542 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,10 @@ Class containing a parsed `.osu` file, ready to be passed to difficulty and perf The constructor takes an object of type `Uint8Array | string` representing the content of a `.osu` file and throws an error if decoding the beatmap fails. +⚠️WARNING⚠️ + +Due to [some current JavaScript oddities](https://github.com/rustwasm/wasm-bindgen/issues/3917), Wasm is not always able to track down objects' lifetime meaning it is possible that memory of unused instances might not get cleared automatically. Hence, to not risk leaking memory, it is recommended to free `Beatmap` instances manually when they're no longer needed with the `free(): void` method. + To convert a beatmap use the `convert(GameMode): void` method. `Beatmap` provides various getters: @@ -228,6 +232,9 @@ const currAttrs = new rosu.Performance({ }).calculate(maxAttrs); // Re-using previous attributes to speed up the calculation. console.log(`PP: ${currAttrs.pp}/${maxAttrs.pp} | Stars: ${maxAttrs.difficulty.stars}`); + +// Free the beatmap manually to avoid risking memory leakage. +map.free(); ``` ### Gradual calculation @@ -274,6 +281,8 @@ while (gradualPerf.nRemaining > 0) { console.log(`PP: ${gradualPerf.next(state)?.pp}`); j += 1; } + +map.free(); ``` ## Installing rosu-pp-js From 270706f7d1e156d62eca5d8f189c9f359506934b Mon Sep 17 00:00:00 2001 From: MaxOhn Date: Wed, 10 Jul 2024 10:21:40 +0200 Subject: [PATCH 2/2] add doc snippet as well --- rosu_pp_js.d.ts | 3 +++ src/beatmap.rs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/rosu_pp_js.d.ts b/rosu_pp_js.d.ts index 86beb2a..bd7f256 100644 --- a/rosu_pp_js.d.ts +++ b/rosu_pp_js.d.ts @@ -249,6 +249,9 @@ export interface ScoreState { /** * All beatmap data that is relevant for difficulty and performance * calculation. +* +* It is recommended to call the method `Beatmap.free` on instances that are +* no longer in use to avoid the risk of leaking memory. */ export class Beatmap { free(): void; diff --git a/src/beatmap.rs b/src/beatmap.rs index e264c77..6f26910 100644 --- a/src/beatmap.rs +++ b/src/beatmap.rs @@ -22,6 +22,9 @@ use crate::{ /// All beatmap data that is relevant for difficulty and performance /// calculation. +/// +/// It is recommended to call the method `Beatmap.free` on instances that are +/// no longer in use to avoid the risk of leaking memory. #[wasm_bindgen(js_name = Beatmap)] pub struct JsBeatmap { pub(crate) inner: Beatmap,