Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable ChangeNotifier clients to dispatch event of object creation in constructor. #133060

Merged
merged 13 commits into from
Aug 23, 2023

Conversation

polina-c
Copy link
Contributor

@polina-c polina-c commented Aug 22, 2023

It is needed to debug notDisposed leaks, where creation call-stack is used to locate owner of the leaking instance.

This PR makes one of the clients invoking the method. Others will start doing this in following PRs.

Peer PR in leak_tracker: dart-lang/leak_tracker#130

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos labels Aug 22, 2023
@polina-c polina-c marked this pull request as draft August 22, 2023 17:23
@flutter-dashboard
Copy link

This pull request has been changed to a draft. The currently pending flutter-gold status will not be able to resolve until a new commit is pushed or the change is marked ready for review again.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@polina-c polina-c marked this pull request as ready for review August 22, 2023 18:22
examples/api/lib/widgets/shortcuts/shortcuts.1.dart Outdated Show resolved Hide resolved
packages/flutter/lib/src/foundation/change_notifier.dart Outdated Show resolved Hide resolved
_creationDispatched = true;
}

mayBeDispatchObjectCreation();
Copy link
Member

Choose a reason for hiding this comment

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

Is the dart tree shaker smart enough to shake out this entire method call when kFlutterMemoryAllocationsEnabled is disabled (i.e. in release mode)?

Copy link
Member

Choose a reason for hiding this comment

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

Same question for all other call sides.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I created app that extends ChangeNotifier, and analyzed its content when kFlutterMemoryAllocationsEnabled is false, in two cases:

  1. When method maybeDispatchObjectCreation checks kFlutterMemoryAllocationsEnabled in condition (this PR)
  2. When there is no check in the method

First option did not contain MemoryAllocations, while second one contained.

So, yes, tree shaker is smart enough.

Copy link
Member

Choose a reason for hiding this comment

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

But does it also eliminate the extra method call of calling mayBeDispatchObjectCreation or do you still pay for that unnecessarily in release mode?

Copy link
Contributor Author

@polina-c polina-c Aug 23, 2023

Choose a reason for hiding this comment

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

For some reasons I do not see this method in both cases, even when I invoke it from the app. Can it be optimized to become part of constructor that invokes it?

Anyway, this PR does not change anything from perspective of this code. It just moves existing code from one place to another.

How about merging this PR to unblock investigation for existing leaks, and then researching what is happening with the method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Per discussion, updated invocations to be under the flag kFlutterMemoryAllocationsEnabled

Copy link
Member

@goderbauer goderbauer left a comment

Choose a reason for hiding this comment

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

LGTM

engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 24, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 24, 2023
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Aug 24, 2023
flutter/flutter@bd836cc...deeb811

2023-08-24 zanderso@users.noreply.github.com Reverts "Roll Flutter Engine from aa98a9d2e86f to 3b92bb6eda19 (4 revisions) (#133224)" (flutter/flutter#133255)
2023-08-24 engine-flutter-autoroll@skia.org Roll Flutter Engine from aa98a9d2e86f to 3b92bb6eda19 (4 revisions) (flutter/flutter#133224)
2023-08-24 engine-flutter-autoroll@skia.org Roll Flutter Engine from 67e8b825cc91 to aa98a9d2e86f (4 revisions) (flutter/flutter#133220)
2023-08-24 engine-flutter-autoroll@skia.org Roll Flutter Engine from 27d75f6221d2 to 67e8b825cc91 (1 revision) (flutter/flutter#133214)
2023-08-23 katelovett@google.com Remove deprecated PlatformViewsService.synchronizeToNativeViewHierarchy (flutter/flutter#133175)
2023-08-23 engine-flutter-autoroll@skia.org Roll Flutter Engine from 58dc868c26cb to 27d75f6221d2 (14 revisions) (flutter/flutter#133211)
2023-08-23 katelovett@google.com Remove deprecated MaterialButtonWithIconMixin (flutter/flutter#133173)
2023-08-23 polinach@google.com Enable ChangeNotifier clients to dispatch event of object creation in constructor. (flutter/flutter#133060)
2023-08-23 goderbauer@google.com Enable literal_only_boolean_expressions (flutter/flutter#133186)
2023-08-23 47866232+chunhtai@users.noreply.github.com Add android analyzer commands for applinks (flutter/flutter#131009)
2023-08-23 reidbaker@google.com Bump memory usage in gradle for platform views (flutter/flutter#133155)
2023-08-23 christopherfujino@gmail.com [flutter_tools] Fix legacy version file not being ensured (flutter/flutter#133097)
2023-08-23 goderbauer@google.com Remove deprecated *TestValues from TestWindow (flutter/flutter#131098)
2023-08-23 engine-flutter-autoroll@skia.org Roll Flutter Engine from ac352ba95f43 to 58dc868c26cb (2 revisions) (flutter/flutter#133165)
2023-08-23 mdebbar@google.com [web] Remove unnecessary lint ignore (flutter/flutter#133164)
2023-08-23 jacksongardner@google.com Add `--experimental-wasm-type-reflection` and support newer emscripten builds. (flutter/flutter#133084)
2023-08-23 engine-flutter-autoroll@skia.org Roll Flutter Engine from 72a06427bd37 to ac352ba95f43 (2 revisions) (flutter/flutter#133158)

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 rmistry@google.com,stuartmorgan@google.com,ychris@google.com 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://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
/// Make sure to invoke it with condition `if (kFlutterMemoryAllocationsEnabled) ...`
/// so that the method is tree-shaken away when the flag is false.
@protected
void maybeDispatchObjectCreation() {
Copy link
Contributor

@passsy passsy Sep 16, 2023

Choose a reason for hiding this comment

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

Adding a new public method to ChangeNotifier is a breaking change for all users that implement ChangeNotifier

class ChangeNotifier2 implements ChangeNotifier {
  // ...
}

Last year, there was a similar breaking change, which was converted to a static method instead to make it non-breaking.

https://github.com/flutter/flutter/blame/e1428bffd464082e8853dcfa54756be0812a900e/packages/flutter/lib/src/foundation/change_notifier.dart#L173C3-L176

Can we avoid the breaking change here somehow?

Copy link
Member

Choose a reason for hiding this comment

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

@passsy You're right, we missed this in our review. It would be great if you (or whoever else suffers under this) could donate some tests to https://github.com/flutter/tests so next time somebody attempts to add a method here tests are failing to remind us that we shouldn't add new methods here.

@polina-c Can you please look into how we can refactor this without causing pain to people that implement ChangeNotifier?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

passsy added a commit to wiredashio/wiredash-sdk that referenced this pull request Sep 17, 2023
@polina-c polina-c added the a: leak tracking Issues and PRs related to memory leaks detected by leak_tracker label Sep 21, 2023
passsy added a commit to wiredashio/wiredash-sdk that referenced this pull request Oct 12, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 15, 2023
passsy added a commit to wiredashio/wiredash-sdk that referenced this pull request Nov 28, 2023
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 autosubmit Merge PR when tree becomes green via auto submit App d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants