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

@ -2117,6 +2117,16 @@ impl Window {
None
})
}
/// Asynchronously load an asset, if the asset hasn't finished loading or doesn't exist this will return None.
/// Your view will not be re-drawn once the asset has finished loading.
///
/// Note that the multiple calls to this method will only result in one `Asset::load` call at a
/// time.
pub fn get_asset<A: Asset>(&mut self, source: &A::Source, cx: &mut App) -> Option<A::Output> {
let (task, _) = cx.fetch_asset::<A>(source);
task.clone().now_or_never()
}
/// Obtain the current element offset. This method should only be called during the
/// prepaint phase of element drawing.
pub fn element_offset(&self) -> Point<Pixels> {
@ -2876,14 +2886,18 @@ impl Window {
}
/// Executes the provided function with the specified image cache.
pub(crate) fn with_image_cache<F, R>(&mut self, image_cache: AnyImageCache, f: F) -> R
pub fn with_image_cache<F, R>(&mut self, image_cache: Option<AnyImageCache>, f: F) -> R
where
F: FnOnce(&mut Self) -> R,
{
self.image_cache_stack.push(image_cache);
let result = f(self);
self.image_cache_stack.pop();
result
if let Some(image_cache) = image_cache {
self.image_cache_stack.push(image_cache);
let result = f(self);
self.image_cache_stack.pop();
result
} else {
f(self)
}
}
/// Sets an input handler, such as [`ElementInputHandler`][element_input_handler], which interfaces with the