Skip to content

Dispose timeline and memory controllers properly - #1523

Merged
DaveShuckerow merged 12 commits into
flutter:masterfrom
DaveShuckerow:dispose-controllers
Jan 15, 2020
Merged

DaveShuckerow merged 12 commits into
flutter:masterfrom
DaveShuckerow:dispose-controllers

Conversation

@DaveShuckerow

Copy link
Copy Markdown
Contributor

This involves a change to the autodispose mixin that allows it to safely remove a callback from an already-disposed listener.

try {
_listenables[i].removeListener(_listeners[i]);
} on FlutterError catch (e) {
// If the listenable has already been disposed, it will throw an error

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.

we should catch the underlying error instead of hiding it with a try catch block.

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.

can we fix the order of execution instead so that the listeners are removed before the listenables are disposed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I like both of these ideas a lot.

we should catch the underlying error instead of hiding it with a try catch block.
The underlying error can change. In debug mode it will be a FlutterError thrown by an assert. In release mode, it will be a null pointer error because the underlying notifier's _listeners list is null.

can we fix the order of execution instead so that the listeners are removed before the listenables are disposed?

We can by overriding the InheritedElement behavior. I originally felt like that was going to be a bit too much boilerplate in the Controllers class, and it will be a few lines more of code than the try/catch. However, having slept on it I feel a lot better about delaying the disposal than I feel about this try/catch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

@jacob314 jacob314 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.

lgtm

@DaveShuckerow

Copy link
Copy Markdown
Contributor Author

would oldWidget.data work here rather than caching _oldData?

oldWidget only exists as a parameter to the didUpdateWidget call. We have to cache something intermediately to dispose of the data later.

Is the notifyClients method sufficient to catch cases where the Element is removed from the tree?

Yes.

Element life cycles are strictly greater than widget/state life cycles. When the element is removed from the tree, all the widgets that depended on it will be gone.

@jacob314

Copy link
Copy Markdown
Contributor

would oldWidget.data work here rather than caching _oldData?

oldWidget only exists as a parameter to the didUpdateWidget call. We have to cache something intermediately to dispose of the data later.

Is the notifyClients method sufficient to catch cases where the Element is removed from the tree?

Yes.

Element life cycles are strictly greater than widget/state life cycles. When the element is removed from the tree, all the widgets that depended on it will be gone.

Great!

@DaveShuckerow
DaveShuckerow merged commit 0274844 into flutter:master Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants