Skip to content

[web] Undisposed images leak ImageSource and Pictures leak on GC #197242

Description

@kevmoo

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.

  • EngineImage and _StaticEngineCodec omit GC ImageSource release: EngineImage retains imageSource per wrapper and clone (including EngineImageShader clones), while CountedRef.onDispose only disposes BackendImage. _StaticEngineCodec lacks a UniqueRef finalizer altogether and lacks an if (_disposed) return; guard in dispose().
  • FinalizationRegistry heldValue -> target retention cycle on Picture: NativeMemoryFinalizer.attach registers CountedRef as weak target and UniqueRef as strong heldValue. Because constructor initializing formals ([this.imageTracker]) are only in scope in initializer lists, constructor-body onDispose closures in SkwasmPicture.fromHandle (even when imageTracker == null) and CkPicture (when imageTracker != null) capture this -> CountedRef, preventing V8 GC from ever finalizing undisposed pictures.
  • Non-idempotent backend image disposal: CkImageDelegate.dispose() and SkwasmImage.dispose() lack disposal guards.

Proposed Fix

  • Tie a single imageSource.retain() / release() pair to EngineImage.box (CountedRef), and wrap _StaticEngineCodec's ImageSource in UniqueRef<ImageSource> with idempotent dispose() across EngineCodec, CkImageDelegate, and SkwasmImage.
  • Construct UniqueRef onDispose callbacks via static helpers in EngineImage, _StaticEngineCodec, CkPicture, and SkwasmPicture so heldValue never captures this.
Detailed reproduction & V8 GC / WAT output (AI-assisted)
  1. Run a browser unit test on f25fa15a19b under Chrome 145 with --js-flags=--expose-gc, allocating and dropping undisposed EngineImage, EngineCodec.staticImage, and ui.Picture instances inside @pragma('dart2js:noInline') @pragma('wasm:never-inline') helpers followed by window.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
  1. Inspecting compiled dart2js JS (CkPicture_closure) and dart2wasm WAT (<SkwasmPicture.fromHandle-constructor-body-context>) confirms $this / (ref null $SkwasmPicture) is stored in the UniqueRef._onDispose closure struct.
  2. Note on MakeLazyImageFromImageBitmap: its second parameter is bool hasPremultipliedAlpha (contrary to the comment in canvaskit/renderer.dart#L261-L265), so CanvasKit never auto-closes ImageBitmap on GPU upload.

Activity

  1. added
    in triage
    engine
    c: performance
    platform-web
    team-web
    and removed
    in triage
    on Nov 24, 2026
  2. added a commit that references this issue on Nov 24, 2026
    5cf905b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: performanceengineplatform-webteam-web

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions