Skip to content

Memory Leak: _ScreenshotPaintingContext.toImage does not dispose created _ScreenshotContainerLayer. #135827

Closed

Description

@ksokolovskyi

Is there an existing issue for this?

Steps to reproduce

_ScreenshotPaintingContext.toImage does not dispose created _ScreenshotContainerLayer instance.

Test to reveal the leak:

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('_ScreenshotPaintingContext.toImage leak',
        (WidgetTester tester) async {
      final TestWidgetInspectorService service = TestWidgetInspectorService();

      WidgetInspectorService.instance = service;

      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;

      final ui.Image? screenshot1 = await service.screenshot(
        repaintBoundary,
        width: 300.0,
        height: 300.0,
      );
      addTearDown(() => screenshot1?.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: 2
            objects:
              _ScreenshotContainerLayer:
                test: _ScreenshotPaintingContext.toImage leak
                identityHashCode: 557158822
                context:
                  start: >
#6_______flutterEventToLeakTracker_
test_widgets.dart:24
#7______MemoryAllocations.dispatchObjectEvent_
memory_allocations.dart:241
#8______MemoryAllocations.dispatchObjectCreated_
memory_allocations.dart:275
#9______new_Layer_
layer.dart:143
#10_____new_ContainerLayer_(package:flutter/src/rendering/layer.dart)
layer.dart:1
#11_____new_OffsetLayer_(package:flutter/src/rendering/layer.dart)
layer.dart:1
#12_____new__ScreenshotContainerLayer_(package:flutter/src/widgets/widget_inspector.dart)
widget_inspector.dart:1
#13_____new__ScreenshotData_
widget_inspector.dart:339
#14______ScreenshotPaintingContext.toImage_
widget_inspector.dart:563
#15_____WidgetInspectorService.screenshot_
widget_inspector.dart:2096
#16______TestWidgetInspectorService.runTests.<anonymous_closure>_
widget_inspector_1_test.dart:45
                    <asynchronous_suspension>
#17_____testWidgetsWithLeakTracking.wrappedCallBack_
test_widgets.dart:127
                    <asynchronous_suspension>
#18_____testWidgets.<anonymous_closure>.<anonymous_closure>_
widget_tester.dart:168
                    <asynchronous_suspension>
#19_____TestWidgetsFlutterBinding._runTestBody_
binding.dart:1013
                    <asynchronous_suspension>
#20_____StackZoneSpecification._registerCallback.<anonymous_closure>_
stack_zone_specification.dart:114
                    <asynchronous_suspension>
              _ProxyLayer:
                test: _ScreenshotPaintingContext.toImage leak
                identityHashCode: 232357060
                context:
                  start: >
#6_______flutterEventToLeakTracker_
test_widgets.dart:24
#7______MemoryAllocations.dispatchObjectEvent_
memory_allocations.dart:241
#8______MemoryAllocations.dispatchObjectCreated_
memory_allocations.dart:275
#9______new_Layer_
layer.dart:143
#10_____new__ProxyLayer_(package:flutter/src/widgets/widget_inspector.dart)
widget_inspector.dart:1
#11______ScreenshotPaintingContext.toImage_
widget_inspector.dart:574
#12_____WidgetInspectorService.screenshot_
widget_inspector.dart:2096
#13______TestWidgetInspectorService.runTests.<anonymous_closure>_
widget_inspector_1_test.dart:45
                    <asynchronous_suspension>
#14_____testWidgetsWithLeakTracking.wrappedCallBack_
test_widgets.dart:127
                    <asynchronous_suspension>
#15_____testWidgets.<anonymous_closure>.<anonymous_closure>_
widget_tester.dart:168
                    <asynchronous_suspension>
#16_____TestWidgetsFlutterBinding._runTestBody_
binding.dart:1013
                    <asynchronous_suspension>
#17_____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>

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