Skip to content

Memory Leak: _RouterState doesn't dispose _RestorableRouteInformation #134205

@ksokolovskyi

Description

@ksokolovskyi

Is there an existing issue for this?

Steps to reproduce

PR that marks the issue: #133803

  1. _RouterState creates _RestorableRouteInformation:

    final _RestorableRouteInformation _routeInformation = _RestorableRouteInformation();

  2. _RouterState doesn't dispose _routeInformation:

    @override
    void dispose() {
    widget.routeInformationProvider?.removeListener(_handleRouteInformationProviderNotification);
    widget.backButtonDispatcher?.removeCallback(_handleBackButtonDispatcherNotification);
    widget.routerDelegate.removeListener(_handleRouterDelegateNotification);
    _currentRouterTransaction = null;
    super.dispose();
    }

Please note that the simple dispose() of _routeInformation is not working.

Tests on Mac work well but on the web fail: https://logs.chromium.org/logs/flutter/buildbucket/cr-buildbucket/8770691241593985297/+/u/run_test.dart_for_web_canvaskit_tests_shard_and_subshard_1/test_stdout#:~:text=A%20_RestorableRouteInformation%20was%20used%20after%20being%20disposed.

The test fails on the web with the next error: A _RestorableRouteInformation was used after being disposed. Once you have called dispose() on a _RestorableRouteInformation, it can no longer be used..
This happens while trying to access the value of _routeInformation in _reportRouteInformation() method.

void _reportRouteInformation(Duration timestamp) {
assert(_routeInformationReportingTaskScheduled);
_routeInformationReportingTaskScheduled = false;
if (_routeInformation.value != null) {
final RouteInformation currentRouteInformation = _routeInformation.value!;
assert(_currentIntentionToReport != null);
widget.routeInformationProvider!.routerReportsNewRouteInformation(currentRouteInformation, type: _currentIntentionToReport!);
}
_currentIntentionToReport = RouteInformationReportingType.none;
}

Expected results

_RouterState disposes _routeInformation.

Actual results

_RouterState doesn't dispose _routeInformation.

Code sample

Test which reveals the leak
testWidgetsWithLeakTracking('WidgetsApp.router leak', (WidgetTester tester) async {
  final SimpleNavigatorRouterDelegate delegate = SimpleNavigatorRouterDelegate(
    builder: (BuildContext context, RouteInformation information) {
      return Text(information.uri.toString());
    },
    onPopPage: (Route<Object?> route, Object? result, SimpleNavigatorRouterDelegate delegate) => true,
  );
  addTearDown(delegate.dispose);

  await tester.pumpWidget(
    WidgetsApp.router(
      routerDelegate: delegate,
      routeInformationParser: SimpleRouteInformationParser(),
      color: const Color(0xFF123456),
    )
  );
});

Flutter Doctor output

Doctor output
[!] Flutter (Channel [user-branch], 3.14.0-14.0.pre.151, on macOS 13.0.1 22A400 darwin-arm64, locale en-GB)
    • Flutter version 3.14.0-14.0.pre.151 on channel master at /Users/ksokolovskyi/dev/flutter_master
    • FLUTTER_GIT_URL = [email protected]:ksokolovskyi/flutter.git
    • Framework revision b06d4d01bf (25 minutes ago), 2023-09-07 11:46:41 +0200
    • Engine revision 5a45ecd24a
    • Dart version 3.2.0 (build 3.2.0-134.0.dev)
    • DevTools version 2.27.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 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.81.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.140

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listf: routesNavigator, Router, and related APIs.frameworkflutter/packages/flutter repository. See also f: labels.team-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