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

@ -1,9 +1,9 @@
use futures::FutureExt;
use gpui::{
App, AppContext, Application, Asset as _, AssetLogger, Bounds, ClickEvent, Context, ElementId,
Entity, HashMapImageCache, ImageAssetLoader, ImageCache, ImageCacheProvider, KeyBinding, Menu,
MenuItem, SharedString, TitlebarOptions, Window, WindowBounds, WindowOptions, actions, div,
hash, image_cache, img, prelude::*, px, rgb, size,
Entity, ImageAssetLoader, ImageCache, ImageCacheProvider, KeyBinding, Menu, MenuItem,
RetainAllImageCache, SharedString, TitlebarOptions, Window, WindowBounds, WindowOptions,
actions, div, hash, image_cache, img, prelude::*, px, rgb, size,
};
use reqwest_client::ReqwestClient;
use std::{collections::HashMap, sync::Arc};
@ -14,7 +14,7 @@ struct ImageGallery {
image_key: String,
items_count: usize,
total_count: usize,
image_cache: Entity<HashMapImageCache>,
image_cache: Entity<RetainAllImageCache>,
}
impl ImageGallery {
@ -44,8 +44,8 @@ impl Render for ImageGallery {
.text_color(gpui::white())
.child("Manually managed image cache:")
.child(
image_cache(self.image_cache.clone()).child(
div()
.image_cache(self.image_cache.clone())
.id("main")
.font_family(".SystemUIFont")
.text_color(gpui::black())
@ -95,7 +95,7 @@ impl Render for ImageGallery {
.map(|ix| img(format!("{}-{}", image_url, ix)).size_20()),
),
),
))
)
.child(
"Automatically managed image cache:"
)
@ -282,7 +282,7 @@ fn main() {
image_key: "".into(),
items_count: IMAGES_IN_GALLERY,
total_count: 0,
image_cache: HashMapImageCache::new(ctx),
image_cache: RetainAllImageCache::new(ctx),
})
})
.unwrap();