Skip to content

Memory Leak: WidgetInspectorServiceExtensions.screenshot does not dispose created ui.Image. #135824

Closed

Description

@ksokolovskyi

Is there an existing issue for this?

Steps to reproduce

WidgetInspectorServiceExtensions.screenshot does not dispose created ui.Image instance.

Test to reveal the leak:

import 'dart:convert';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

import 'widget_inspector_test_utils.dart';

void main() {
  _TestWidgetInspectorService.runTests();
}

class _TestWidgetInspectorService extends TestWidgetInspectorService {
  // These tests need access to protected members of WidgetInspectorService.
  static void runTests() {
    final TestWidgetInspectorService service = TestWidgetInspectorService();
    WidgetInspectorService.instance = service;

    testWidgetsWithLeakTracking('Leak', (WidgetTester tester) async {
      await tester.pumpWidget(
        const Center(
          child: RepaintBoundary(
            child: ColoredBox(
              color: Colors.red,
              child: Padding(
                padding: EdgeInsets.all(100.0),
                child: SizedBox(
                  height: 100.0,
                  width: 100.0,
                ),
              ),
            ),
          ),
        ),
      );

      final Element repaintBoundary =
          find.byType(RepaintBoundary).evaluate().single;

      // Verify we get the same image if we go through the service extension
      // instead of invoking the screenshot method directly.
      final Future<Object?> base64ScreenshotFuture = service.testExtension(
        WidgetInspectorServiceExtensions.screenshot.name,
        <String, String>{
          'id': service.toId(repaintBoundary, 'group')!,
          'width': '300.0',
          'height': '300.0',
        },
      );

      final TestWidgetsFlutterBinding binding =
          TestWidgetsFlutterBinding.ensureInitialized();
      final ui.Image screenshot = (await binding.runAsync<ui.Image>(() async {
        final String base64Screenshot =
            (await base64ScreenshotFuture)! as String;
        final ui.Codec codec =
            await ui.instantiateImageCodec(base64.decode(base64Screenshot));
        final ui.FrameInfo frame = await codec.getNextFrame();
        return frame.image;
      }))!;
      addTearDown(screenshot.dispose);
    }, leakTrackingTestConfig: LeakTrackingTestConfig.debugNotDisposed());
  }
}
Test output
Expected: leak free
  Actual: <Instance of 'Leaks'>
   Which: contains leaks:
          # The text is generated by leak_tracker.
          # For leak troubleshooting tips open:
          # https://github.com/dart-lang/leak_tracker/blob/main/doc/TROUBLESHOOT.md
          notDisposed:
            total: 1
            objects:
              Image:
                test: leak
                identityHashCode: 13383695
                context:
                  start: >
#6_______flutterEventToLeakTracker_
test_widgets.dart:24
#7______MemoryAllocations.dispatchObjectEvent_
memory_allocations.dart:241
#8______MemoryAllocations._imageOnCreate_
memory_allocations.dart:315
#9______new_Image.__(dart:ui/painting.dart:1717:15)
#10______NativeScene.toImage.<anonymous_closure>.<anonymous_closure>_(dart:ui/compositing.dart:71:26)
                    #11_____FfiTrampoline___toImage$Method$FfiNative$Ptr_(dart:ffi)
#12______NativeScene._toImage_(dart:ui/compositing.dart:78:20)
#13______NativeScene.toImage.<anonymous_closure>_(dart:ui/compositing.dart:67:54)
#14______futurize_(dart:ui/painting.dart:6946:35)
#15______NativeScene.toImage_(dart:ui/compositing.dart:67:12)
#16_____OffsetLayer.toImage_
layer.dart:1554
#17______ScreenshotPaintingContext.toImage_
widget_inspector.dart:615
#18_____WidgetInspectorService.screenshot_
widget_inspector.dart:2096
#19_____WidgetInspectorService.initServiceExtensions.<anonymous_closure>_
widget_inspector.dart:1287
#20_____TestWidgetInspectorService.testExtension_
widget_inspector_test_utils.dart:100
#21______TestWidgetInspectorService.runTests.<anonymous_closure>_
widget_inspector_1_test.dart:43
                    <asynchronous_suspension>
#22_____testWidgetsWithLeakTracking.wrappedCallBack_
test_widgets.dart:127
                    <asynchronous_suspension>
#23_____testWidgets.<anonymous_closure>.<anonymous_closure>_
widget_tester.dart:168
                    <asynchronous_suspension>
#24_____TestWidgetsFlutterBinding._runTestBody_
binding.dart:1013
                    <asynchronous_suspension>
#25_____StackZoneSpecification._registerCallback.<anonymous_closure>_
stack_zone_specification.dart:114
                    <asynchronous_suspension>
          
          


package:matcher                                                  expect
expect
widget_tester.dart:458
_tearDownTestingWithLeakTracking
test_widgets.dart:82
===== asynchronous gap ===========================
dart:async                                                       _CustomZone.registerBinaryCallback
configureLeakTrackingTearDown.<fn>
test_widgets.dart:59

Flutter Doctor output

Doctor output
[✓] Flutter (Channel master, 3.14.0-14.0.pre.580, on macOS 13.0.1 22A400 darwin-arm64, locale en-GB)
    • Flutter version 3.14.0-14.0.pre.580 on channel master at /Users/ksokolovskyi/dev/flutter_master
    • Upstream repository [email protected]:ksokolovskyi/flutter.git
    • FLUTTER_GIT_URL = [email protected]:ksokolovskyi/flutter.git
    • Framework revision ba1d6147ba (28 hours ago), 2023-09-30 04:52:24 -0400
    • Engine revision f726438d4d
    • Dart version 3.2.0 (build 3.2.0-218.0.dev)
    • DevTools version 2.28.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at /Users/ksokolovskyi/Library/Android/sdk
    • Platform android-34, build-tools 33.0.1
    • ANDROID_HOME = /Users/ksokolovskyi/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] IntelliJ IDEA Community Edition (version 2023.2)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.82.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.72.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 13.0.1 22A400 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 117.0.5938.92

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Activity

  1. ksokolovskyi commented on Oct 1, 2023

    @ksokolovskyi
    ContributorAuthor
  2. added
    a: leak trackingIssues and PRs related to memory leaks detected by leak_tracker
    on Oct 1, 2023
  3. github-actions commented on Oct 16, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  4. locked as resolved and limited conversation to collaborators on Oct 16, 2023
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

    a: leak trackingIssues and PRs related to memory leaks detected by leak_tracker

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions