Skip to content

Memory leak: SearchAnchor does not dispose created FocusNode and SearchController. #136117

@ksokolovskyi

Description

@ksokolovskyi

Is there an existing issue for this?

Steps to reproduce

SearchAnchor does not dispose created SearchController and FocusNode.

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';

void main() {
  testWidgetsWithLeakTracking(
    'SearchAnchor leaks',
    (WidgetTester tester) async {
      Widget buildAnchor({Widget? viewLeading}) {
        return MaterialApp(
          home: Material(
            child: SearchAnchor(
              viewLeading: viewLeading,
              builder: (
                BuildContext context,
                SearchController controller,
              ) {
                return IconButton(
                  icon: const Icon(Icons.search),
                  onPressed: () {
                    controller.openView();
                  },
                );
              },
              suggestionsBuilder: (
                BuildContext context,
                SearchController controller,
              ) {
                return <Widget>[];
              },
            ),
          ),
        );
      }

      await tester.pumpWidget(buildAnchor());
      await tester.tap(find.widgetWithIcon(IconButton, Icons.search));
      await tester.pumpAndSettle();
      // Default is a icon button with arrow_back.
      expect(find.widgetWithIcon(IconButton, Icons.arrow_back), findsOneWidget);

      await tester.pumpWidget(Container());
      await tester.pumpWidget(
        buildAnchor(viewLeading: const Icon(Icons.history)),
      );
      await tester.tap(find.widgetWithIcon(IconButton, Icons.search));
      await tester.pumpAndSettle();
      expect(find.byIcon(Icons.arrow_back), findsNothing);
      expect(find.byIcon(Icons.history), findsOneWidget);
    },
  );
}
Test output
✓ SearchAnchor leaks
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:
              FocusNode:
                test: SearchAnchor leaks
                identityHashCode: 508987169
              SearchController:
                test: SearchAnchor leaks
                identityHashCode: 173793760
          
          

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.668, on macOS 13.0.1 22A400 darwin-arm64, locale en-GB)
    • Flutter version 3.14.0-14.0.pre.668 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 f0970365ca (15 hours ago), 2023-10-06 12:30:20 -0600
    • Engine revision 59b6b94e1a
    • Dart version 3.2.0 (build 3.2.0-231.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.13.0

[✓] 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.83.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.74.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.149

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

• No issues found!

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: leak trackingIssues and PRs related to memory leaks detected by leak_trackerf: material designflutter/packages/flutter/material repository.found in release: 3.16Found to occur in 3.16frameworkflutter/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-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions