Skip to content

Conversation

@Dimilkalathiya
Copy link
Contributor

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

- Fixes particular test not handling text controller dispose
@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Apr 10, 2024
@Dimilkalathiya Dimilkalathiya changed the title - Fixes state leaking text controller - Fixes _DropdownMenuState leaking text controller Apr 10, 2024
@Dimilkalathiya
Copy link
Contributor Author

cc: @polina-c

currentHighlight = widget.enableSearch ? i : null;
widget.onSelected?.call(entry.value);
_localTextEditingController?.dispose();
_localTextEditingController = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need to dispose _localTextEditingController on every click? It will cause recreation of the controller.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This getter recreates controller if there is no controller:

  TextEditingController get _textEditingController {
    return widget.controller ?? (_localTextEditingController ??= TextEditingController());
  }

Copy link
Contributor Author

@Dimilkalathiya Dimilkalathiya Apr 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, having getter which creates disposable class is quite troublesome even if we fix this issue if bounds to cause issue if new feature is introduced

i'll try to remove getter and minimise instantiation limited to initState and didUpdateWidget

selection: TextSelection.collapsed(offset: entry.label.length),
);
widget.onSelected?.call(entry.value);
_localTextEditingController?.dispose();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same comment here.

It will cause recreate of controller on each onEditingComplete.

@Dimilkalathiya
Copy link
Contributor Author

Hii @polina-c, Have updated PR as per discussed, let me know if this is good or you'd have some improvement in mind 🤔

@polina-c
Copy link
Contributor

Thanks.
It would be great to understand what was the reason for original leak.

Originally the getter was creating local controller if one in widget is not provided.
If widget's controller was changed, local controller was disposed and nulled
Then local controller was disposed in dispose.
Why this was not working as expected?

If we understand what was wrong, we may find smaller code change for fix. Or, we may discover some bigger bug in Flutter Framework, that is always a good thing.

@Dimilkalathiya
Copy link
Contributor Author

Dimilkalathiya commented Apr 14, 2024

Hii @polina-c Yes, its nice idea to understand what is causing this bug, so i did some test and it turns out that MenuItemButton calls onSelected event after post frame callback.

Screenshot 2024-04-14 at 2 01 41 PM

which is there because of copy-paste issue #118731 so it can change focus.

which causes onSelected to called after dispose which creates TextController thanks to getter we had.

if we simply remove post frame callback function and call onSelected function directly it fixes issue but that mean it would reopen #118731 again.

some additional details that might help:-

  • If controller is passed from outside user it doesn't cause leak since we are not disposing controller provided by user
  • and since we are using null aware operator ? while using on onPressed on MenuItemButton on current PR it just ignores adding last event on TextEditingController and simply calls onSelected if user has not provided their own controller which make outside behaviour stay same and fixes issue, if user did have passed controller it will add event on both TextEditingController and call onSelected since we aren't disposing user provided controller.
  • i think MenuItemButton is only made and used for DropdownMenu only so it makes sense to have post frame callback to fix issue mentioned earlier and adding changes we made to in this PR would be good fix for leak.

but what do you think? we should merge this PR? or we should be changing MenuItemButton logic?

@polina-c
Copy link
Contributor

Thank you for research!
It is not good that onSelected is called after dispose. And it is even worse that it triggers creation of a new controller.
This PR does not fix the first problem, but fixes the second. So, yes, let's merge it.

@polina-c polina-c merged commit 1002ce4 into flutter:master Apr 14, 2024
@polina-c polina-c added the a: leak tracking Issues and PRs related to memory leaks detected by leak_tracker label Apr 14, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 14, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 14, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 15, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 15, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 15, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 15, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 15, 2024
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Apr 15, 2024
Manual roll requested by [email protected]

flutter/flutter@53cba24...2e748e8

2024-04-15 [email protected] Implementing control flow collections (flutter/flutter#146601)
2024-04-15 [email protected] Roll Packages from 78f684c to 6698b2d (3 revisions) (flutter/flutter#146761)
2024-04-15 [email protected] test: Fix memory leak in transitions test (flutter/flutter#146747)
2024-04-15 [email protected] Fix filled text field active indicator overflows container bounds (flutter/flutter#146637)
2024-04-14 [email protected] - Fixes _DropdownMenuState leaking text controller (flutter/flutter#146571)
2024-04-13 [email protected] Fix memory leaks in `FloatingActionButton` (flutter/flutter#146711)
2024-04-12 [email protected] Roll Flutter Engine from 0e56e3dffe43 to 1a13c7d1f40e (2 revisions) (flutter/flutter#146703)
2024-04-12 [email protected] Roll pub packages (flutter/flutter#146704)
2024-04-12 49699333+dependabot[bot]@users.noreply.github.com Bump peter-evans/create-pull-request from 6.0.2 to 6.0.3 (flutter/flutter#146702)
2024-04-12 [email protected] Avoid forwarding the data after socket is disconnected. (flutter/flutter#146665)
2024-04-12 [email protected] [flutter_tools] Fix conductor for package args roll (flutter/flutter#146646)
2024-04-12 [email protected] Roll Flutter Engine from 6b37b170998e to 0e56e3dffe43 (4 revisions) (flutter/flutter#146698)
2024-04-12 [email protected] Light sliver clean up before SliverTree (flutter/flutter#146696)
2024-04-12 [email protected] Fix label text color is wrong for a focused and hovered TextField (flutter/flutter#146572)
2024-04-12 [email protected] Fix `getOffsetForCaret` crash (flutter/flutter#146669)
2024-04-12 [email protected] Update gen_keycodes templates (flutter/flutter#146481)
2024-04-12 [email protected] Support `flutter run --wasm` and `flutter drive --wasm`. (flutter/flutter#146231)
2024-04-12 [email protected] Fix curved animation memory leak for scrollbar (flutter/flutter#146670)
2024-04-12 [email protected] Roll Packages from e98839a to 78f684c (6 revisions) (flutter/flutter#146691)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
@Dimilkalathiya Dimilkalathiya deleted the fix145622 branch April 21, 2024 07:13
gilnobrega pushed a commit to gilnobrega/flutter that referenced this pull request Apr 22, 2024
TecHaxter pushed a commit to TecHaxter/flutter_packages that referenced this pull request May 22, 2024
)

Manual roll requested by [email protected]

flutter/flutter@53cba24...2e748e8

2024-04-15 [email protected] Implementing control flow collections (flutter/flutter#146601)
2024-04-15 [email protected] Roll Packages from 78f684c to 6698b2d (3 revisions) (flutter/flutter#146761)
2024-04-15 [email protected] test: Fix memory leak in transitions test (flutter/flutter#146747)
2024-04-15 [email protected] Fix filled text field active indicator overflows container bounds (flutter/flutter#146637)
2024-04-14 [email protected] - Fixes _DropdownMenuState leaking text controller (flutter/flutter#146571)
2024-04-13 [email protected] Fix memory leaks in `FloatingActionButton` (flutter/flutter#146711)
2024-04-12 [email protected] Roll Flutter Engine from 0e56e3dffe43 to 1a13c7d1f40e (2 revisions) (flutter/flutter#146703)
2024-04-12 [email protected] Roll pub packages (flutter/flutter#146704)
2024-04-12 49699333+dependabot[bot]@users.noreply.github.com Bump peter-evans/create-pull-request from 6.0.2 to 6.0.3 (flutter/flutter#146702)
2024-04-12 [email protected] Avoid forwarding the data after socket is disconnected. (flutter/flutter#146665)
2024-04-12 [email protected] [flutter_tools] Fix conductor for package args roll (flutter/flutter#146646)
2024-04-12 [email protected] Roll Flutter Engine from 6b37b170998e to 0e56e3dffe43 (4 revisions) (flutter/flutter#146698)
2024-04-12 [email protected] Light sliver clean up before SliverTree (flutter/flutter#146696)
2024-04-12 [email protected] Fix label text color is wrong for a focused and hovered TextField (flutter/flutter#146572)
2024-04-12 [email protected] Fix `getOffsetForCaret` crash (flutter/flutter#146669)
2024-04-12 [email protected] Update gen_keycodes templates (flutter/flutter#146481)
2024-04-12 [email protected] Support `flutter run --wasm` and `flutter drive --wasm`. (flutter/flutter#146231)
2024-04-12 [email protected] Fix curved animation memory leak for scrollbar (flutter/flutter#146670)
2024-04-12 [email protected] Roll Packages from e98839a to 78f684c (6 revisions) (flutter/flutter#146691)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
@polina-c polina-c changed the title - Fixes _DropdownMenuState leaking text controller - Fixes _DropdownMenuState leaking text controller [prod-leak-fix] Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: leak tracking Issues and PRs related to memory leaks detected by leak_tracker f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants