diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 27a3fcd9..fb83a837 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -3446,6 +3446,7 @@ impl Context { /// Release all memory and textures related to the given image URI. /// /// If you attempt to load the image again, it will be reloaded from scratch. + /// Also this cancels any ongoing loading of the image. pub fn forget_image(&self, uri: &str) { use load::BytesLoader as _; diff --git a/crates/egui_extras/src/loaders/file_loader.rs b/crates/egui_extras/src/loaders/file_loader.rs index 9feaebf5..d13134e2 100644 --- a/crates/egui_extras/src/loaders/file_loader.rs +++ b/crates/egui_extras/src/loaders/file_loader.rs @@ -95,10 +95,15 @@ impl BytesLoader for FileLoader { } Err(err) => Err(err.to_string()), }; - let prev = cache.lock().insert(uri.clone(), Poll::Ready(result)); - assert!(matches!(prev, Some(Poll::Pending)), "unexpected state"); - ctx.request_repaint(); - log::trace!("finished loading {uri:?}"); + let mut cache = cache.lock(); + if let std::collections::hash_map::Entry::Occupied(mut entry) = cache.entry(uri.clone()) { + let entry = entry.get_mut(); + *entry = Poll::Ready(result); + ctx.request_repaint(); + log::trace!("finished loading {uri:?}"); + } else { + log::trace!("cancelled loading {uri:?}\nNote: This can happen if `forget_image` is called while the image is still loading."); + } } }) .expect("failed to spawn thread");