Fix memory leaks in WidgetInspector and WidgetInspectorService. (#135828)

This commit is contained in:
Kostia Sokolovskyi 2023-10-02 04:41:46 +02:00 committed by GitHub
parent 71acf24201
commit cfe7afdc50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 305 additions and 205 deletions

View File

@ -372,6 +372,12 @@ class _ScreenshotData {
set screenshotOffset(Offset offset) {
containerLayer.offset = offset;
}
/// Releases allocated resources.
@mustCallSuper
void dispose() {
containerLayer.dispose();
}
}
/// A place to paint to build screenshots of [RenderObject]s.
@ -550,7 +556,7 @@ class _ScreenshotPaintingContext extends PaintingContext {
Rect renderBounds, {
double pixelRatio = 1.0,
bool debugPaint = false,
}) {
}) async {
RenderObject repaintBoundary = renderObject;
while (!repaintBoundary.isRepaintBoundary) {
repaintBoundary = repaintBoundary.parent!;
@ -604,7 +610,15 @@ class _ScreenshotPaintingContext extends PaintingContext {
// been called successfully for all layers in the regular scene.
repaintBoundary.debugLayer!.buildScene(ui.SceneBuilder());
return data.containerLayer.toImage(renderBounds, pixelRatio: pixelRatio);
final ui.Image image;
try {
image = await data.containerLayer.toImage(renderBounds, pixelRatio: pixelRatio);
} finally {
data.dispose();
}
return image;
}
}
@ -1285,6 +1299,7 @@ mixin WidgetInspectorService {
return <String, Object?>{'result': null};
}
final ByteData? byteData = await image.toByteData(format:ui.ImageByteFormat.png);
image.dispose();
return <String, Object>{
'result': base64.encoder.convert(Uint8List.view(byteData!.buffer)),
@ -3141,7 +3156,7 @@ class _InspectorOverlayLayer extends Layer {
_InspectorOverlayRenderState? _lastState;
/// Picture generated from _lastState.
late ui.Picture _picture;
ui.Picture? _picture;
TextPainter? _textPainter;
double? _textPainterMaxWidth;
@ -3150,6 +3165,7 @@ class _InspectorOverlayLayer extends Layer {
void dispose() {
_textPainter?.dispose();
_textPainter = null;
_picture?.dispose();
super.dispose();
}
@ -3184,9 +3200,10 @@ class _InspectorOverlayLayer extends Layer {
if (state != _lastState) {
_lastState = state;
_picture?.dispose();
_picture = _buildPicture(state);
}
builder.addPicture(Offset.zero, _picture);
builder.addPicture(Offset.zero, _picture!);
}
ui.Picture _buildPicture(_InspectorOverlayRenderState state) {

File diff suppressed because it is too large Load Diff