Repository navigation
Done
Labels
c: performanceRelates to speed or footprint issues (see "perf:" labels)engineflutter/engine related. See also e: labels.platform-webWeb applications specificallyteam-webOwned by Web platform team
Description
Garbage-collecting an undisposed EngineImage or _StaticEngineCodec never calls imageSource.release(), leaking DOM ImageBitmap and <img> memory on CanvasKit and single-threaded Skwasm. Separately, constructor closures in SkwasmPicture and image-bearing CkPicture instances capture this inside UniqueRef, preventing FinalizationRegistry from collecting undisposed pictures.
EngineImageand_StaticEngineCodecomit GCImageSourcerelease:EngineImageretainsimageSourceper wrapper and clone (includingEngineImageShaderclones), whileCountedRef.onDisposeonly disposesBackendImage._StaticEngineCodeclacks aUniqueReffinalizer altogether and lacks anif (_disposed) return;guard indispose().FinalizationRegistryheldValue -> targetretention cycle onPicture:NativeMemoryFinalizer.attachregistersCountedRefas weaktargetandUniqueRefas strongheldValue. Because constructor initializing formals ([this.imageTracker]) are only in scope in initializer lists, constructor-bodyonDisposeclosures inSkwasmPicture.fromHandle(even whenimageTracker == null) andCkPicture(whenimageTracker != null) capturethis -> CountedRef, preventing V8 GC from ever finalizing undisposed pictures.- Non-idempotent backend image disposal:
CkImageDelegate.dispose()andSkwasmImage.dispose()lack disposal guards.
Proposed Fix
- Tie a single
imageSource.retain()/release()pair toEngineImage.box(CountedRef), and wrap_StaticEngineCodec'sImageSourceinUniqueRef<ImageSource>with idempotentdispose()acrossEngineCodec,CkImageDelegate, andSkwasmImage. - Construct
UniqueRefonDisposecallbacks viastatichelpers inEngineImage,_StaticEngineCodec,CkPicture, andSkwasmPicturesoheldValuenever capturesthis.
Detailed reproduction & V8 GC / WAT output (AI-assisted)
- Run a browser unit test on
f25fa15a19bunder Chrome145with--js-flags=--expose-gc, allocating and dropping undisposedEngineImage,EngineCodec.staticImage, andui.Pictureinstances inside@pragma('dart2js:noInline') @pragma('wasm:never-inline')helpers followed bywindow.gc()+ macrotask turns:
REAL_V8_GC_UI [canvaskit]: engineImageClosed=false, staticCodecClosed=false, pictureClosed=false
REAL_V8_GC_UI [skwasm]: engineImageClosed=false, staticCodecClosed=false, pictureClosed=false
REAL_V8_GC_SKWASM_PICTURE: baseline=0, afterGc=1 // C++ flutter::DisplayList leaked even with imageTracker == null
- Inspecting compiled
dart2jsJS (CkPicture_closure) anddart2wasmWAT (<SkwasmPicture.fromHandle-constructor-body-context>) confirms$this/(ref null $SkwasmPicture)is stored in theUniqueRef._onDisposeclosure struct. - Note on
MakeLazyImageFromImageBitmap: its second parameter isbool hasPremultipliedAlpha(contrary to the comment incanvaskit/renderer.dart#L261-L265), so CanvasKit never auto-closesImageBitmapon GPU upload.
Reacted by Veli Bacik
Metadata
Metadata
Assignees
Labels
c: performanceRelates to speed or footprint issues (see "perf:" labels)engineflutter/engine related. See also e: labels.platform-webWeb applications specificallyteam-webOwned by Web platform team
Activity
[web] Release ImageSource on GC and fix Picture finalizer cycles (flu…