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

@ -3,7 +3,9 @@ use std::sync::Arc;
use editor::{Editor, EditorMode, MultiBuffer};
use futures::future::Shared;
use gpui::{App, Entity, Hsla, Task, TextStyleRefinement, prelude::*};
use gpui::{
App, Entity, Hsla, RetainAllImageCache, Task, TextStyleRefinement, image_cache, prelude::*,
};
use language::{Buffer, Language, LanguageRegistry};
use markdown_preview::{markdown_parser::parse_markdown, markdown_renderer::render_markdown_block};
use nbformat::v4::{CellId, CellMetadata, CellType};
@ -148,6 +150,7 @@ impl Cell {
MarkdownCell {
markdown_parsing_task,
image_cache: RetainAllImageCache::new(cx),
languages: languages.clone(),
id: id.clone(),
metadata: metadata.clone(),
@ -329,6 +332,7 @@ pub trait RunnableCell: RenderableCell {
pub struct MarkdownCell {
id: CellId,
metadata: CellMetadata,
image_cache: Entity<RetainAllImageCache>,
source: String,
parsed_markdown: Option<markdown_preview::markdown_elements::ParsedMarkdown>,
markdown_parsing_task: Task<()>,
@ -403,12 +407,12 @@ impl Render for MarkdownCell {
.child(self.gutter(window, cx))
.child(
v_flex()
.image_cache(self.image_cache.clone())
.size_full()
.flex_1()
.p_3()
.font_ui(cx)
.text_size(TextSize::Default.rems(cx))
//
.children(parsed.children.iter().map(|child| {
div().relative().child(div().relative().child(
render_markdown_block(child, &mut markdown_render_context),

View file

@ -1,5 +1,7 @@
use anyhow::Result;
use gpui::{App, ClipboardItem, Context, Entity, Task, Window, div, prelude::*};
use gpui::{
App, ClipboardItem, Context, Entity, RetainAllImageCache, Task, Window, div, prelude::*,
};
use language::Buffer;
use markdown_preview::{
markdown_elements::ParsedMarkdown, markdown_parser::parse_markdown,
@ -11,6 +13,7 @@ use crate::outputs::OutputContent;
pub struct MarkdownView {
raw_text: String,
image_cache: Entity<RetainAllImageCache>,
contents: Option<ParsedMarkdown>,
parsing_markdown_task: Option<Task<Result<()>>>,
}
@ -33,6 +36,7 @@ impl MarkdownView {
Self {
raw_text: text.clone(),
image_cache: RetainAllImageCache::new(cx),
contents: None,
parsing_markdown_task: Some(task),
}
@ -74,6 +78,7 @@ impl Render for MarkdownView {
markdown_preview::markdown_renderer::RenderContext::new(None, window, cx);
v_flex()
.image_cache(self.image_cache.clone())
.gap_3()
.py_4()
.children(parsed.children.iter().map(|child| {