Skip to content

instantiateImageCodecWithSize is not disposing intermediates on web #147066

@curt-weber

Description

@curt-weber

Steps to reproduce

  1. Run tests below on a non-web platform
  2. Tests should pass
  3. Run tests below on web platform
  4. instantiateImageCodecWithSize will fail

Expected results

Both tests should pass on all platforms. instantiateImageCodecWithSize on web is not disposing of the original imagery following conversion to the target size. cc @polina-c - this is similar to an issue I opened previously, but here are tests demonstrating the leak.

Actual results

Both tests pass on non-web platforms. The second test, which evaluates if instantiateImageCodecWithSize is leaking, fails on web.

Code sample

Code sample
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter_test/flutter_test.dart';

void main() {
  late final Uint8List bytes;
  final loaded = <Image>{};

  setUpAll(() async {
    final image = await createTestImage(width: 500, height: 500);
    final data = await image.toByteData(format: ImageByteFormat.png);
    bytes = data!.buffer.asUint8List();
    image.dispose();
    Image.onCreate = loaded.add;
    Image.onDispose = loaded.remove;
  });

  tearDownAll(() {
    Image.onCreate = null;
    Image.onDispose = null;
  });

  tearDown(loaded.clear);

  test('instantiateImageCodec', () async {
    expect(loaded.length, 0);
    final codec = await instantiateImageCodec(bytes);
    final frame = await codec.getNextFrame();
    expect(loaded.length, 1);
    frame.image.dispose();
    codec.dispose();
    expect(loaded.length, 0);
  });

  test('instantiateImageCodecWithSize', () async {
    expect(loaded.length, 0);
    final codec = await instantiateImageCodecWithSize(
      await ImmutableBuffer.fromUint8List(bytes),
      getTargetSize: (_, __) => const TargetImageSize(width: 250, height: 250),
    );
    final frame = await codec.getNextFrame();
    expect(loaded.length, 1);
    frame.image.dispose();
    codec.dispose();
    expect(loaded.length, 0);
  });
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

No response

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.19.5, on Microsoft Windows [Version 10.0.19044.4170], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.3.5)
[√] Android Studio (version 2021.3)
[√] VS Code (version 1.88.1)
[√] Connected device (3 available)
[√] Network resources

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: leak trackingIssues and PRs related to memory leaks detected by leak_trackera: tests"flutter test", flutter_test, or one of our testse: web_canvaskitCanvasKit (a.k.a. Skia-on-WebGL) rendering backend for Webfound in release: 3.19Found to occur in 3.19found in release: 3.22Found to occur in 3.22frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onperf: memoryPerformance issues related to memoryplatform-webWeb applications specificallyr: fixedIssue is closed as already fixed in a newer versionteam-webOwned by Web platform teamtriaged-webTriaged by Web platform team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions