Repository navigation
Description
Current behavior
An ImageSource that outlives the elements using it keeps every Image and ImageBrush that ever used it alive, along with everything they reference. Examples are an icon kept in a static field or in app resources, or a shared BitmapImage.
- The source holds its subscribers strongly.
ImageSource.crossruntime.cskeeps them in a strongList<Action<ImageData>>. Image(Skia) never unsubscribes on unload. It subscribes with_sourceDisposable.Disposable = source.Subscribe(img => ...), and the callback captures theImage. It only unsubscribes when itsSourcechanges. It has no unload handling.- 6.8 adds two more strong handlers for SVG sources. There,
Image.skia.csalso subscribes to theSvgImageSource'sSourceLoadedandOpenFailedevents, which are released the same way. ImageBrushis held twice. It subscribes to its source with_sourceSubscription, and it also addsImageSource.Invalidated += brush.OnInvalidateRender. The brush in turn keeps the element it painted alive.
Measured in a WebAssembly app on 6.8 whose file type icons are SvgImageSources kept in static fields. After browsing documents for a while, they held 176 handlers and, through them, about 30 MB of pages the user had already left. A GC heap dump showed this chain:
static SvgImageSource -> OpenFailed -> Image -> binding -> template -> page
Expected behavior
A source does not keep its consumers alive. An Image or ImageBrush that is otherwise unreferenced is collected, whatever its source.
Status of the fixes
- master:
Image: fix(imaging): Release a decoded image when nothing displays it #25124 fixes it, because anImagesubscribes to its source only while it's loaded. That PR includes a regression test with a bitmap source and an SVG source, each shared by two Images.ImageBrush: still affected with fix(imaging): Release a decoded image when nothing displays it #25124.
- 6.8:
- The fix: fix(imaging): Release a decoded image when nothing displays it #25124 is too large to backport, so a narrow fix is proposed for
servicing/6.8. - How it works:
ImageSourceholds its subscribers weakly, and each subscriber keeps its own callback alive through the disposable it already holds. The SVG events andImageBrush'sInvalidatedhandler go through weak forwarders. - What doesn't change: when sources open and decode, and when
ImageOpenedandImageFailedare raised.
- The fix: fix(imaging): Release a decoded image when nothing displays it #25124 is too large to backport, so a narrow fix is proposed for
There is a separate path not covered here. A shareable object keeps the first object it's set on as its inheritance-context parent (_associatedParent) until a second user appears or the property is cleared. So a source used by only one element at a time still keeps that element alive. #25102 addresses this on master.
How to reproduce
- Keep an
SvgImageSourceorBitmapImagein a static field, and load it (SetSourceAsync). - Show it in two
Images, then remove them from the tree. - Collect and wait for pending finalizers.
- Both
Images are still alive, held through the source's subscriber list (and, on 6.8 for SVG, through itsSourceLoadedandOpenFailedevents). Using twoImages sidesteps the_associatedParentpath above.
Activity
test(imaging): Collect Images their source outlives
test(imaging): Collect Images their source outlives
fix(imaging): Let a long-lived source drop the brushes it painted
fix(imaging): Make weak source subscriptions opt-in
fix(imaging): Hold image source subscribers weakly