Skip to content

Commit

Permalink
fix: 🐛 memory leak when LottieRenderer is created (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf authored Jan 13, 2025
1 parent ef3219e commit c5fa543
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug demo-player",
"program": "${workspaceFolder}/examples/demo-player/target/debug/demo-player",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
14 changes: 6 additions & 8 deletions dotlottie-rs/src/dotlottie_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,15 +815,13 @@ impl DotLottieRuntime {
let theme_exists = self
.manifest()
.and_then(|manifest| manifest.themes.as_ref())
.map_or(false, |themes| {
themes.iter().any(|theme| theme.id == theme_id)
});
.is_some_and(|themes| themes.iter().any(|theme| theme.id == theme_id));

if !theme_exists {
return false;
}

let can_set_theme = self.manifest().map_or(false, |manifest| {
let can_set_theme = self.manifest().is_some_and(|manifest| {
manifest.animations.iter().any(|animation| {
animation.themes.is_none()
|| animation
Expand Down Expand Up @@ -1584,11 +1582,11 @@ impl DotLottiePlayer {
}

pub fn clear(&self) {
self.player.write().unwrap().clear();
self.player.write().unwrap().clear()
}

pub fn set_config(&self, config: Config) {
self.player.write().unwrap().set_config(config);
self.player.write().unwrap().set_config(config)
}

pub fn speed(&self) -> f32 {
Expand Down Expand Up @@ -1672,7 +1670,7 @@ impl DotLottiePlayer {
}

pub fn subscribe(&self, observer: Arc<dyn Observer>) {
self.player.write().unwrap().subscribe(observer);
self.player.write().unwrap().subscribe(observer)
}

pub fn state_machine_subscribe(&self, observer: Arc<dyn StateMachineObserver>) -> bool {
Expand Down Expand Up @@ -1709,7 +1707,7 @@ impl DotLottiePlayer {
}

pub fn unsubscribe(&self, observer: &Arc<dyn Observer>) {
self.player.write().unwrap().unsubscribe(observer);
self.player.write().unwrap().unsubscribe(observer)
}

pub fn set_theme(&self, theme_id: &str) -> bool {
Expand Down
7 changes: 6 additions & 1 deletion dotlottie-rs/src/lottie_renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ pub trait LottieRenderer {

impl dyn LottieRenderer {
pub fn new<R: Renderer>(renderer: R) -> Box<Self> {
let mut renderer = renderer;
let background_shape = R::Shape::default();

renderer.push(Drawable::Shape(&background_shape)).unwrap();

Box::new(LottieRendererImpl {
animation: R::Animation::default(),
background_shape: R::Shape::default(),
background_shape,
renderer,
buffer: vec![],
width: 0,
Expand Down

0 comments on commit c5fa543

Please sign in to comment.