Skip to content

create gesture recognizers on attach and dispose on detach to avoid leaks - #42076

Merged
Piinks merged 2 commits into
flutter:masterfrom
jaumard:bugfix/gestureRecognizersLeak
Oct 17, 2019
Merged

Piinks merged 2 commits into
flutter:masterfrom
jaumard:bugfix/gestureRecognizersLeak

Conversation

@jaumard

@jaumard jaumard commented Oct 6, 2019 •

Copy link
Copy Markdown
Contributor

Description

Create gesture recognizer on attach and dispose them on dispose to avoid leaks on RenderEditable

Related Issues

fix #39636

Tests

I added the following tests:

Didn't added any as it's only internal fields, let me know if I can add some and how.

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require Flutter developers to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (Please read Handling breaking changes). Replace this with a link to the e-mail where you asked for input on this proposed change.
  • No, this is not a breaking change.

@fluttergithubbot

Copy link
Copy Markdown
Contributor

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.

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

@fluttergithubbot fluttergithubbot added the framework flutter/packages/flutter repository. See also f: labels. label Oct 6, 2019
@Piinks Piinks added a: quality A truly polished experience f: gestures flutter/packages/flutter/gestures repository. labels Oct 7, 2019
@chunhtai

chunhtai commented Oct 7, 2019

Copy link
Copy Markdown
Contributor

First of all, Welcome and thank you for contributing to flutter !

The change looks good, but we would like to have test to make sure no one break it in the future. Can you add a test for this change?

@jaumard

jaumard commented Oct 7, 2019

Copy link
Copy Markdown
Contributor Author

Thanks :)
Like I said on the description, I don't mind adding test, but I don't know how/what to test here. It's only internal stuff on the class that has moved and wasn't tested previously.
Can you explain me what kind of tests are expected ? I would gladly add them

@chunhtai

chunhtai commented Oct 7, 2019 •

Copy link
Copy Markdown
Contributor

Yes, it seems extreme hard to test this method.... Someone might have a better idea

I can only think of an indirect way of testing this

test('editale test', () {
    final TextSelectionDelegate delegate = FakeEditableTextState();
    final RenderEditable editable = RenderEditable(
      backgroundCursorColor: Colors.grey,
      selectionColor: Colors.black,
      textDirection: TextDirection.ltr,
      cursorColor: Colors.red,
      offset: ViewportOffset.zero(),
      textSelectionDelegate: delegate,
      text: const TextSpan(
        text: 'test',
        style: TextStyle(
          height: 1.0, fontSize: 10.0, fontFamily: 'Ahem',
        ),
      ),
      startHandleLayerLink: LayerLink(),
      endHandleLayerLink: LayerLink(),
      selection: const TextSelection(
        baseOffset: 0,
        extentOffset: 3,
        affinity: TextAffinity.upstream,
      ),
      onSelectionChanged: (_, __, ___) { },
    );
    final PipelineOwner owner = PipelineOwner(onNeedVisualUpdate: () { });
    // Implement CustomTestWidgetsFlutterBinding that implement a spy into
    // GestureBinding.instance.pointerRouter you can use TestWidgetsFlutterBinding
    // as template
    CustomTestWidgetsFlutterBinding.ensureInitialized();
    editable.attach(owner);
    // This should register pointer into GestureBinding.instance.pointerRouter.
    editable.handleEvent(const PointerDownEvent(), BoxHitTestEntry(editable, const Offset(10,10)));
    editable.detach();
    // Make sure GestureBinding.instance.pointerRouter is removed
    // ...
  });

When Rendereditable handle a pointer event it will use taprecognizer to register the pointer into GestureBinding.instance.pointerRouter.
In theory we can check GestureBinding.instance.pointerRouter is empty after rendereditable is detached. However, there is no public api to spy into what is in the tracked pointer in GestureBinding.instance.pointerRouter. What you can do is to override the GestureBinding that can spy into GestureBinding.instance.pointerRouter.

Let me know if any of the code above is unclear.

@chunhtai

chunhtai commented Oct 7, 2019

Copy link
Copy Markdown
Contributor

@jaumard
jaumard force-pushed the bugfix/gestureRecognizersLeak branch from f590209 to 09db9bb Compare October 15, 2019 20:10
@jaumard

jaumard commented Oct 15, 2019

Copy link
Copy Markdown
Contributor Author

@chunhtai ok I finally was able to make that test !! I was struggling to spy the GestureBinding. Let me know if it's ok

@chunhtai chunhtai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice test!
I left some minor styling comment. overall all looks good.

@jaumard
jaumard force-pushed the bugfix/gestureRecognizersLeak branch 2 times, most recently from 0274895 to 6b5f238 Compare October 16, 2019 12:36
@jaumard

jaumard commented Oct 16, 2019

Copy link
Copy Markdown
Contributor Author

@chunhtai I have a problem, look like _GestureBindingSpy() have to be called before the test start, but if I do so all other tests are failing.
What I need is a setUp(() => _GestureBindingSpy()); but only for my test and not the other.
Is there a way to achieve this ? Or should I move my tests into it's own file (editable_gesture_test.dart) ? Let me know :)

@jaumard
jaumard force-pushed the bugfix/gestureRecognizersLeak branch from 6b5f238 to 605eb92 Compare October 16, 2019 13:57
@jaumard

jaumard commented Oct 16, 2019

Copy link
Copy Markdown
Contributor Author

I've push it on another file to be sure all the CI builds were green :) all good

@goderbauer goderbauer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good with two nits.

@jaumard
jaumard force-pushed the bugfix/gestureRecognizersLeak branch from 605eb92 to f58da36 Compare October 17, 2019 06:24
@jaumard
jaumard force-pushed the bugfix/gestureRecognizersLeak branch from f58da36 to 7d1430a Compare October 17, 2019 06:37
@jaumard

jaumard commented Oct 17, 2019

Copy link
Copy Markdown
Contributor Author

@goderbauer I have made the changes, but look like some builds need to be restart (errors doesn't look like happening because of my changes)

@goderbauer goderbauer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

I restarted the build for you.

@Piinks
Piinks merged commit 1faf6a9 into flutter:master Oct 17, 2019
@Piinks

Piinks commented Oct 17, 2019

Copy link
Copy Markdown
Contributor

Thank you again for the contribution @jaumard ! 🎉

@jaumard
jaumard deleted the bugfix/gestureRecognizersLeak branch October 18, 2019 18:43
Inconnu08 pushed a commit to Inconnu08/flutter that referenced this pull request Nov 26, 2019
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

a: quality A truly polished experience f: gestures flutter/packages/flutter/gestures repository. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RenderEditable leaks its gesture recognizers

6 participants