Skip to content

Memory Leak: _InspectorOverlayLayer does not dispose created ui.Picture. #135826

@ksokolovskyi

Description

@ksokolovskyi

Is there an existing issue for this?

Steps to reproduce

_InspectorOverlayLayer does not dispose created ui.Picture instance.

Test to reveal the leak:

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('_InspectorOverlayLayer leak',
        (WidgetTester tester) async {
      final GlobalKey selectButtonKey = GlobalKey();
      final GlobalKey inspectorKey = GlobalKey();

      await tester.pumpWidget(
        Directionality(
          textDirection: TextDirection.ltr,
          child: WidgetInspector(
            key: inspectorKey,
            selectButtonBuilder:
                (BuildContext context, VoidCallback onPressed) {
              return Material(
                child: ElevatedButton(
                  onPressed: onPressed,
                  key: selectButtonKey,
                  child: null,
                ),
              );
            },
            child: Material(
              child: Center(
                child: ElevatedButton(
                  onPressed: () {},
                  child: const Text('TOP'),
                ),
              ),
            ),
          ),
        ),
      );

      expect(WidgetInspectorService.instance.selection.current, isNull);

      await tester.tap(find.text('TOP'), warnIfMissed: false);
      await tester.pump();

      expect(WidgetInspectorService.instance.selection.current, isNotNull);
    }, 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:
              _NativePicture:
                test: _InspectorOverlayLayer leak
                identityHashCode: 393545165
                context:
                  start: >
#6_______flutterEventToLeakTracker_
test_widgets.dart:24
#7______MemoryAllocations.dispatchObjectEvent_
memory_allocations.dart:241
#8______MemoryAllocations._pictureOnCreate_
memory_allocations.dart:323
#9_______NativePictureRecorder.endRecording_(dart:ui/painting.dart:6435:23)
#10______InspectorOverlayLayer._buildPicture_
widget_inspector.dart:3256
#11______InspectorOverlayLayer.addToScene_
widget_inspector.dart:3203
#12_____Layer._addToSceneWithRetainedRendering_
layer.dart:712
#13_____ContainerLayer.addChildrenToScene_
layer.dart:1378
#14_____TransformLayer.addToScene_
layer.dart:1995
#15_____ContainerLayer.buildScene_
layer.dart:1130
#16_____RenderView.compositeFrame_
view.dart:252
#17_____AutomatedTestWidgetsFlutterBinding.drawFrame_
binding.dart:1419
#18_____RendererBinding._handlePersistentFrameCallback_
binding.dart:457
#19_____SchedulerBinding._invokeFrameCallback_
binding.dart:1325
#20_____SchedulerBinding.handleDrawFrame_
binding.dart:1255
#21_____AutomatedTestWidgetsFlutterBinding.pump.<anonymous_closure>_
binding.dart:1264
#22______rootRun_(dart:async/zone.dart:1399:13)
#23______CustomZone.run_(dart:async/zone.dart:1301:19)
#24_____TestAsyncUtils.guard_
test_async_utils.dart:71
#25_____AutomatedTestWidgetsFlutterBinding.pump_
binding.dart:1251
#26_____WidgetTester.pump.<anonymous_closure>_
widget_tester.dart:637
#27______rootRun_(dart:async/zone.dart:1399:13)
#28______CustomZone.run_(dart:async/zone.dart:1301:19)
#29_____TestAsyncUtils.guard_
test_async_utils.dart:71
#30_____WidgetTester.pump_

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!

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