# Added missing `dispose()` for some disposable objects in Flutter

> 'dispose()' might fail because of double disposal.




:::important
These breaking change docs are accurate, as of the release
under which they are published. Over time, the
workarounds described here might become inaccurate.
We don't, in general, keep these breaking change docs up
to date as of each release.

这些破坏性改动文档的准确性仅限于其发布时对应的版本。
随着时间的推移，文档中描述的方案可能会逐渐失效。
通常情况下，我们不会在每次发布新版本时都对这些文档进行同步更新。

The [breaking change index file](/release/breaking-changes)
lists the docs created for each release.

[破坏性改动列表](/release/breaking-changes) 
列出了每个版本的文档。

:::


## 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.

```plaintext
Once you have called dispose() on a <class name>, it can no longer be used.
```

Code before migration:

```dart
x.dispose();
```

Code after migration:

```dart
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

See the progress and status [in the tracking issue](https://github.com/flutter/flutter/issues/134787).

