Use image cache to stop leaking images (#29452)

This PR fixes several possible memory leaks due to loading images in
markdown files and the image viewer, using the new image cache APIs

TODO: 
- [x] Ensure this didn't break rendering in any of the affected
components.

Release Notes:

- Fixed several image related memory leaks
This commit is contained in:
Mikayla Maki 2025-04-29 12:30:16 -07:00 committed by GitHub
parent d732a7d361
commit 4758173c33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 181 additions and 46 deletions

View file

@ -35,9 +35,19 @@ impl ImageView {
pub fn new(
image_item: Entity<ImageItem>,
project: Entity<Project>,
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
cx.subscribe(&image_item, Self::on_image_event).detach();
cx.on_release_in(window, |this, window, cx| {
let image_data = this.image_item.read(cx).image.clone();
if let Some(image) = image_data.clone().get_render_image(window, cx) {
cx.drop_image(image, None);
}
image_data.remove_asset(cx);
})
.detach();
Self {
image_item,
project,
@ -234,7 +244,9 @@ impl SerializableItem for ImageView {
.update(cx, |project, cx| project.open_image(project_path, cx))?
.await?;
cx.update(|_, cx| Ok(cx.new(|cx| ImageView::new(image_item, project, cx))))?
cx.update(
|window, cx| Ok(cx.new(|cx| ImageView::new(image_item, project, window, cx))),
)?
})
}
@ -365,13 +377,13 @@ impl ProjectItem for ImageView {
project: Entity<Project>,
_: &Pane,
item: Entity<Self::Item>,
_: &mut Window,
window: &mut Window,
cx: &mut Context<Self>,
) -> Self
where
Self: Sized,
{
Self::new(item, project, cx)
Self::new(item, project, window, cx)
}
}