Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Potential memory leak issues in node #368

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions js-binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class Resvg {
get width(): number
/** Get the SVG height */
get height(): number
free(): void
}
export class RenderedImage {
/** Write the image data to Buffer */
Expand All @@ -49,4 +50,5 @@ export class RenderedImage {
get width(): number
/** Get the PNG height */
get height(): number
free(): void
}
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ impl RenderedImage {
pub fn height(&self) -> u32 {
self.pix.height()
}

#[cfg(not(target_arch = "wasm32"))]
#[napi]
pub fn free(&mut self) {
self.pix = Pixmap::new(1, 1).expect("Failed to create empty Pixmap");
}
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -268,6 +274,21 @@ impl Resvg {
pub fn height(&self) -> f32 {
self.tree.size.height().round()
}

#[cfg(not(target_arch = "wasm32"))]
#[napi]
pub fn free(&mut self) {
self.tree = usvg::Tree {
size: usvg::Size::from_wh(1.0, 1.0).unwrap(),
view_box: usvg::ViewBox {
rect: usvg::NonZeroRect::from_ltrb(0.0, 0.0, 1.0, 1.0).unwrap(),
aspect: usvg::AspectRatio::default(),
},
root: usvg::Node::new(usvg::NodeKind::Group(usvg::Group::default())),
};

self.js_options = JsOptions::default();
}
}

#[cfg(target_arch = "wasm32")]
Expand Down
Loading