Skip to content

Memory Leak: AnnotatedRegionLayer<SystemUiOverlayStyle> is not disposed when testing with _TestRecordingCanvasMatcher  #134661

@ksokolovskyi

Description

@ksokolovskyi

Is there an existing issue for this?

PR which marks this leak: #134663

Steps to reproduce

It happens that AnnotatedRegionLayer<SystemUiOverlayStyle> is not disposed when testing with _TestRecordingCanvasMatcher.

  1. When running the following text with paintsExactlyCountTimes matcher leak_tracker detects leak:
testWidgetsWithLeakTracking('ListView should paint with offset', (WidgetTester tester) async {
  final ScrollController controller = ScrollController(initialScrollOffset: 120.0);
  addTearDown(controller.dispose);

  await tester.pumpWidget(
    MaterialApp(
      home: Scaffold(
        body: SizedBox(
          height: 500.0,
          child: CustomScrollView(
            controller: controller,
            slivers: <Widget>[
              const SliverAppBar(
                expandedHeight: 250.0,
              ),
              SliverList(
                delegate: ListView.builder(
                  itemExtent: 100.0,
                  itemCount: 100,
                  itemBuilder: (_, __) => const SizedBox(
                    height: 40.0,
                    child: Text('hey'),
                  ),
                ).childrenDelegate,
              ),
            ],
          ),
        ),
      ),
    ),
  );

  final RenderObject renderObject = tester.renderObject(find.byType(Scrollable));
  expect(renderObject, paintsExactlyCountTimes(#drawParagraph, 10));
}, leakTrackingTestConfig: LeakTrackingTestConfig.debugNotDisposed());
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:
                AnnotatedRegionLayer<SystemUiOverlayStyle>:
                  test: ListView should paint with offset
                  identityHashCode: 43646489
                  context:
                    start: >
                      #6_______flutterEventToLeakTracker_(package:leak_tracker_flutter_testing/src/test_widgets.dart:24:23)
                      #7______MemoryAllocations.dispatchObjectEvent_(package:flutter/src/foundation/memory_allocations.dart:238:23)
                      #8______MemoryAllocations.dispatchObjectCreated_(package:flutter/src/foundation/memory_allocations.dart:272:5)
                      #9______new_Layer_(package:flutter/src/rendering/layer.dart:143:34)
                      #10_____new_ContainerLayer_(package:flutter/src/rendering/layer.dart)
                      #11_____new_AnnotatedRegionLayer_(package:flutter/src/rendering/layer.dart)
                      #12_____RenderAnnotatedRegion.paint_(package:flutter/src/rendering/proxy_box.dart:5059:43)
                      #13_____TestRecordingPaintingContext.paintChild_(package:flutter_test/src/recording_canvas.dart:104:11)
                      #14_____RenderProxyBoxMixin.paint_(package:flutter/src/rendering/proxy_box.dart:129:13)
                      #15_____TestRecordingPaintingContext.paintChild_(package:flutter_test/src/recording_canvas.dart:104:11)
                      #16_____RenderSliverPersistentHeader.paint_(package:flutter/src/rendering/sliver_persistent_header.dart:311:15)
                      #17_____TestRecordingPaintingContext.paintChild_(package:flutter_test/src/recording_canvas.dart:104:11)
                      #18_____RenderViewportBase._paintContents_(package:flutter/src/rendering/viewport.dart:690:17)
                      #19_____TestRecordingPaintingContext.pushClipRect.<anonymous_closure>_(package:flutter_test/src/recording_canvas.dart:116:97)
                      #20_____ClipContext._clipAndPaint_(package:flutter/src/painting/clip.dart:25:12)
                      #21_____ClipContext.clipRectAndPaint_(package:flutter/src/painting/clip.dart:53:5)
                      #22_____TestRecordingPaintingContext.pushClipRect_(package:flutter_test/src/recording_canvas.dart:116:5)
                      #23_____RenderViewportBase.paint_(package:flutter/src/rendering/viewport.dart:665:38)
                      #24_____TestRecordingPaintingContext.paintChild_(package:flutter_test/src/recording_canvas.dart:104:11)
                      #25_____RenderProxyBoxMixin.paint_(package:flutter/src/rendering/proxy_box.dart:129:13)
                      #26_____TestRecordingPaintingContext.paintChild_(package:flutter_test/src/recording_canvas.dart:104:11)
                      #27_____RenderProxyBoxMixin.paint_(package:flutter/src/rendering/proxy_box.dart:129:13)
                      #28_____TestRecordingPaintingContext.paintChild_(package:flutter_test/src/recording_canvas.dart:104:11)
                      #29_____RenderProxyBoxMixin.paint_(package:flutter/src/rendering/proxy_box.dart:129:13)
                      #30_____TestRecordingPaintingContext.paintChild_(package:flutter_test/src/recording_canvas.dart:104:11)
                      #31_____RenderProxyBoxMixin.paint_(package:flutter/src/rendering/proxy_box.dart:129:13)
                      #32_____TestRecordingPaintingContext.paintChild_(package:flutter_test/src/recording_canvas.dart:104:11)
                      #33_____RenderProxyBoxMixin.paint_(package:flutter/src/rendering/proxy_box.dart:129:13)
                      #34_____TestRecordingPaintingContext.paintChild_(package:flutter_test/src/recording_canvas.dart:104:11)
                      #35_____RenderProxyBoxMixin.paint_(package:flutter/src/rendering/proxy_box.dart:129:13)
                      #36_____TestRecordingPaintingContext.paintChild_(package:flutter_test/src/recording_canvas.dart:104:11)
                      #37_____RenderProxyBoxMixin.paint_(package:flutter/src/rendering/proxy_box.dart:129:13)
                      #38_____RenderTransform.paint_(package:flutter/src/rendering/proxy_box.dart:2557:17)
                      #39_____TestRecordingPaintingContext.paintChild_(package:flutter_test/src/recording_canvas.dart:104:11)
                      #40_____RenderClipRect.paint_(package:flutter/src/rendering/proxy_box.dart:1552:17)
                      #41______evaluatePainter_(package:flutter_test/src/mock_canvas.dart:537:20)
                      #42______TestRecordingCanvasMatcher.matches_(package:flutter_test/src/mock_canvas.dart:554:12)
                      #43______expect_(package:matcher/src/expect/expect.dart:138:30)
                      #44_____expect_(package:matcher/src/expect/expect.dart:56:3)
                      #45_____expect_(package:flutter_test/src/widget_tester.dart:458:18)
                      #46_____main.<anonymous_closure>_(file:///Users/ksokolovskyi/dev/flutter_master/packages/flutter/test/widgets/list_view_viewporting_test.dart:508:5)
                      <asynchronous_suspension>
                      #47_____testWidgetsWithLeakTracking.wrappedCallBack_(package:leak_tracker_flutter_testing/src/test_widgets.dart:126:5)
                      <asynchronous_suspension>
                      #48_____testWidgets.<anonymous_closure>.<anonymous_closure>_(package:flutter_test/src/widget_tester.dart:168:15)
                      <asynchronous_suspension>
                      #49_____TestWidgetsFlutterBinding._runTestBody_(package:flutter_test/src/binding.dart:1013:5)
                      <asynchronous_suspension>
                      #50_____StackZoneSpecification._registerCallback.<anonymous_closure>_(package:stack_trace/src/stack_zone_specification.dart:114:42)
                      <asynchronous_suspension>
            
            
  
  package:matcher                                                  expect
  package:flutter_test/src/widget_tester.dart 458:18               expect
  package:leak_tracker_flutter_testing/src/test_widgets.dart 81:5  _tearDownTestingWithLeakTracking
  ===== asynchronous gap ===========================
  dart:async                                                       _CustomZone.registerBinaryCallback
  package:leak_tracker_flutter_testing/src/test_widgets.dart 59:9  configureLeakTrackingTearDown.<fn>
  
  1. When running the following text with commented paintsExactlyCountTimes matcher leak_tracker does not detect leak:
testWidgetsWithLeakTracking('ListView should paint with offset', (WidgetTester tester) async {
  final ScrollController controller = ScrollController(initialScrollOffset: 120.0);
  addTearDown(controller.dispose);

  await tester.pumpWidget(
    MaterialApp(
      home: Scaffold(
        body: SizedBox(
          height: 500.0,
          child: CustomScrollView(
            controller: controller,
            slivers: <Widget>[
              const SliverAppBar(
                expandedHeight: 250.0,
              ),
              SliverList(
                delegate: ListView.builder(
                  itemExtent: 100.0,
                  itemCount: 100,
                  itemBuilder: (_, __) => const SizedBox(
                    height: 40.0,
                    child: Text('hey'),
                  ),
                ).childrenDelegate,
              ),
            ],
          ),
        ),
      ),
    ),
  );

  // final RenderObject renderObject = tester.renderObject(find.byType(Scrollable));
  // expect(renderObject, paintsExactlyCountTimes(#drawParagraph, 10));
}, leakTrackingTestConfig: LeakTrackingTestConfig.debugNotDisposed());

Flutter Doctor output

Doctor output
[✓] Flutter (Channel master, 3.14.0-14.0.pre.270, on macOS 13.0.1 22A400 darwin-arm64, locale en-GB)
    • Flutter version 3.14.0-14.0.pre.270 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 622c2b2b07 (75 minutes ago), 2023-09-13 09:35:29 -0400
    • Engine revision 5e671d5c90
    • Dart version 3.2.0 (build 3.2.0-140.0.dev)
    • DevTools version 2.27.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at /Users/ksokolovskyi/Library/Android/sdk
    • Platform android-33, 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.1)
    • 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 116.0.5845.187

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

• 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 testsfound in release: 3.14Found to occur in 3.14frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions