Skip to content

Leak memory in tests when dragging a SingleChildScrollView #169119

@ValentinVignal

Description

@ValentinVignal

Steps to reproduce

  1. flutter test on the sample code

Expected results

The tests should pass

Actual results

The test are failing because of a TestRestorationManager and a RestorationBucket are not disposed

Code sample

Code sample

You can also checkout https://github.com/ValentinVignal/flutter_app_stable/tree/leak-memory/single-child-scrollable

// test/flutter_test_config.dart
import 'dart:async';

import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

Future<void> testExecutable(FutureOr<void> Function() testFunction) async {
  LeakTesting.enable();
  await testFunction();
}
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SingleChildScrollView(
          child: Column(
            children: [for (var i = 0; i < 100; i++) Text('Item $i')],
          ),
        ),
      ),
    );
  }
}

void main() {
  testWidgets('Counter increments smoke test', (WidgetTester tester) async {
    await tester.pumpWidget(MyWidget());

    expect(find.text('Item 0').hitTestable(), findsOne);
    expect(find.text('Item 50').hitTestable(), findsNothing);

    await tester.drag(
      find.byType(SingleChildScrollView),
      const Offset(0, -200),
    );

    await tester.pump();

    expect(find.text('Item 0').hitTestable(), findsNothing);
    expect(find.text('Item 30').hitTestable(), findsOne);
  });
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
00:03 +1 -1: (tearDownAll) [E]                                                                                                                                                                            
  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/leak_tracking/TROUBLESHOOT.md
            notDisposed:
              total: 2
              objects:
                TestRestorationManager:
                  test: Counter increments smoke test
                  identityHashCode: 77264436
                RestorationBucket:
                  test: Counter increments smoke test
                  identityHashCode: 208072902
            
            
  
  package:matcher                                              expect
  package:leak_tracker_testing/src/leak_testing.dart 69:24     LeakTesting.collectedLeaksReporter.<fn>
  package:leak_tracker_flutter_testing/src/testing.dart 72:37  maybeTearDownLeakTrackingForAll
  ===== asynchronous gap ===========================
  dart:async                                                   _CustomZone.registerBinaryCallback
  package:flutter_test/src/test_compat.dart 302:3              _tearDownForTestFile
  

To run this test again: /Users/valentinvignal/development/flutter/bin/cache/dart-sdk/bin/dart test /Users/valentinvignal/Projects/flutter_app_stable/test/widget_test.dart -p vm --plain-name '(tearDownAll)'
00:03 +1 -1: Some tests failed.  

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.29.3, on macOS 15.4.1 24E263 darwin-x64, locale en-US) [915ms]
    • Flutter version 3.29.3 on channel stable at /Users/valentinvignal/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ea121f8859 (6 weeks ago), 2025-04-11 19:10:07 +0000
    • Engine revision cf56914b32
    • Dart version 3.7.2
    • DevTools version 2.42.3

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [3.7s]
    • Android SDK at /Users/valentinvignal/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • ANDROID_HOME = /Users/valentinvignal/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.3+-79915915-b509.11)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 16.0) [4.0s]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16A242d
    ✗ Unable to get list of installed Simulator runtimes.
    ! CocoaPods 1.15.2 out of date (1.16.2 is recommended).
        CocoaPods is a package manager for iOS or macOS platform code.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/to/platform-plugins
      To update CocoaPods, see https://guides.cocoapods.org/using/getting-started.html#updating-cocoapods

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

[✓] Android Studio (version 2024.2) [28ms]
    • 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 21.0.3+-79915915-b509.11)

[✓] VS Code (version 1.100.2) [25ms]
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.110.0

[✓] Connected device (2 available) [6.7s]
    • macOS (desktop) • macos  • darwin-x64     • macOS 15.4.1 24E263 darwin-x64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 136.0.7103.114

[✓] Network resources [452ms]
    • 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 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.29Found to occur in 3.29found in release: 3.33Found to occur in 3.33frameworkflutter/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