Added missing `dispose()` for some disposable objects in Flutter
Summary
#Missing calls to 'dispose()' are added for some disposable objects. For example, ContextMenuController did not dispose OverlayEntry, and EditableTextState did not dispose TextSelectionOverlay.
If some other code also invokes 'dispose()' for the object, and the object is protected from double disposal, the second 'dispose()' fails with the following error message:
Once you have called dispose() on a <class name>, it can no longer be used.
Background
#The convention is that the owner of an object should dispose of it.
This convention was broken in some places:
owners were not disposing the disposable objects.
The issue was fixed by adding a call to dispose()
.
However, if the object is protected from double disposal,
this can cause failures when running in debug mode
and dispose()
is called elsewhere on the object.
Migration guide
#If you encounter the following error, update your code to
call dispose()
only in cases when your code created the object.
Once you have called dispose() on a <class name>, it can no longer be used.
Code before migration:
x.dispose();
Code after migration:
if (xIsCreatedByMe) {
x.dispose();
}
To locate the incorrect disposal, check the call stack of the error. If the call stack points to dispose
in your code, this disposal is incorrect and should be fixed.
If the error occurs in Flutter code, dispose()
was
called incorrectly the first time.
You can locate the incorrect call by temporary calling print(StackTrace.current)
in the body of the failed method dispose
.
Timeline
#除非另有说明,本文档之所提及适用于 Flutter 的最新稳定版本,本页面最后更新时间: 2024-10-09。 查看文档源码 或者 为本页面内容提出建议。