Skip to content

[material/menu_anchor.dart] _MenuAnchorState is not always GC'd #149584

@davidhicks980

Description

@davidhicks980

Steps to reproduce

  1. Run example.
  2. Toggle menu open and closed several times.
  3. View _MenuAnchorState instances in Memory devtools.

PR for fix: #149586

Expected results

There should only be one _MenuAnchorState for each menu anchor

Actual results

Detached _MenuAnchorStates are being retained

Code sample

Code sample
import 'package:flutter/material.dart';

void main() => runApp(const BugApp());

class BugApp extends StatelessWidget {
  const BugApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: Bug());
  }
}

class Bug extends StatefulWidget {
  const Bug({super.key});

  @override
  State<StatefulWidget> createState() => _BugState();
}

class _BugState extends State<Bug> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: MenuAnchor(
          builder: (context, controller, child) {
            return TextButton(
                onPressed: () {
                  if (controller.isOpen) {
                    controller.close();
                  } else {
                    controller.open();
                  }
                },
                child: const Text('Toggle Menu'));
          },
          menuChildren: [
            SubmenuButton(
              menuChildren: [
                MenuItemButton(
                  onPressed: () {},
                  child: const Text('Item 1'),
                ),
                MenuItemButton(
                  onPressed: () {},
                  child: const Text('Item 2'),
                ),
              ],
              child: const Text('Item 2'),
            ),
            SubmenuButton(
              menuChildren: [
                MenuItemButton(
                  onPressed: () {},
                  child: const Text('Item 1'),
                ),
                MenuItemButton(
                  onPressed: () {},
                  child: const Text('Item 2'),
                ),
              ],
              child: const Text('Item 2'),
            ),
          ]),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration
Screen.Recording.2024-06-03.at.6.28.24.AM.mov

Logs

Logs
N/A

Flutter Doctor output

Doctor output I'm running a custom version of Flutter, but this should not affect the bug.
[!] Flutter (Channel master, 3.23.0-13.0.pre.69, on macOS 14.0 23A344 darwin-arm64, locale en-US)
    • Flutter version 3.23.0-13.0.pre.69 on channel master at /Users/davidhicks/development/custom
    ! Warning: `flutter` on your path resolves to /Users/davidhicks/Development/custom/bin/flutter, which
      is not inside your current Flutter SDK checkout at /Users/davidhicks/development/custom. Consider
      adding /Users/davidhicks/development/custom/bin to the front of your path.
    ! Warning: `dart` on your path resolves to /Users/davidhicks/Development/custom/bin/dart, which is
      not inside your current Flutter SDK checkout at /Users/davidhicks/development/custom. Consider
      adding /Users/davidhicks/development/custom/bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 124aacbaef (2 days ago), 2024-05-31 22:32:25 -0400
    • Engine revision d81edf635a
    • Dart version 3.5.0 (build 3.5.0-214.0.dev)
    • DevTools version 2.36.0
    • If those were intentional, you can disregard the above warnings; however it is recommended to use
      "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/davidhicks/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/davidhicks/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    • CocoaPods version 1.15.2

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

[✓] Android Studio (version 2023.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.10+0-17.0.10b1087.21-11572160)

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

[✓] Connected device (3 available)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 14.0 23A344
      darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 14.0 23A344
      darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome
      125.0.6422.113
    ! Error: Browsing on the local area network for David’s iPhone. Ensure the device is unlocked and
      attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

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

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listf: material designflutter/packages/flutter/material repository.found in release: 3.22Found to occur in 3.22found in release: 3.23Found to occur in 3.23frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onperf: memoryPerformance issues related to memoryr: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages teamwaiting for PR to land (fixed)A fix is in flight

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions