Skip to content

Enable dispose-class-fields and dispose-fields lints to resolve memory leaks#9857

Open
kenzieschmoll wants to merge 4 commits into
flutter:masterfrom
kenzieschmoll:memoryleaks
Open

Enable dispose-class-fields and dispose-fields lints to resolve memory leaks#9857
kenzieschmoll wants to merge 4 commits into
flutter:masterfrom
kenzieschmoll:memoryleaks

Conversation

@kenzieschmoll

Copy link
Copy Markdown
Member

Resolves not disposed memory leaks across devtools. Enables two new lints from DCM.

@kenzieschmoll kenzieschmoll requested review from a team, bkonyi and srawlins as code owners June 15, 2026 23:04

class KeyboardShortcutsState extends State<KeyboardShortcuts>
with AutoDisposeMixin {
// ignore: dispose-fields, false positive. Disposed via autoDisposeFocusNode.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@incendial FYI several false positives in this PR when the object is disposed by another layer of abstraction.

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.

Thanks, I've went though all the cases and I believe the vast majority is fixed (the only tricky one left is with applyToFeatureControllers). Fixes will be in 1.38.0 which ships today

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request enables the dispose-class-fields and dispose-fields lints and adds proper disposal logic across various controllers, states, and widgets in DevTools to prevent memory leaks. The review feedback highlights several critical issues and improvement opportunities: manually calling dispose() on a State object should be avoided; a shared DragAndDropManager should not be prematurely disposed of inside an individual widget's lifecycle; potential null pointer exceptions when removing an overlay entry must be resolved; and fields should remain non-nullable rather than being made nullable solely to be nulled out during disposal, which avoids unnecessary null assertions.

Comment thread packages/devtools_app/lib/src/screens/dtd/services.dart Outdated
Comment thread packages/devtools_app/lib/src/shared/table/table.dart Outdated
Comment thread packages/devtools_app/lib/src/screens/dtd/events.dart Outdated
Comment thread packages/devtools_app/lib/src/screens/dtd/events.dart Outdated
static final tabs = [analysisTab, diffTab];

// ignore: dispose-fields, screen controller disposal is handled by the [ScreenControllers] class.
late AppSizeController controller;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@incendial is there also a way to detect when a class is not the owner of an object and therefore should not be responsible for disposing it?

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.

That should be fixed as well

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.

2 participants