Unverified Commit cfe7afdc authored by Kostia Sokolovskyi's avatar Kostia Sokolovskyi Committed by GitHub

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

parent 71acf242
...@@ -372,6 +372,12 @@ class _ScreenshotData { ...@@ -372,6 +372,12 @@ class _ScreenshotData {
set screenshotOffset(Offset offset) { set screenshotOffset(Offset offset) {
containerLayer.offset = offset; containerLayer.offset = offset;
} }
/// Releases allocated resources.
@mustCallSuper
void dispose() {
containerLayer.dispose();
}
} }
/// A place to paint to build screenshots of [RenderObject]s. /// A place to paint to build screenshots of [RenderObject]s.
...@@ -550,7 +556,7 @@ class _ScreenshotPaintingContext extends PaintingContext { ...@@ -550,7 +556,7 @@ class _ScreenshotPaintingContext extends PaintingContext {
Rect renderBounds, { Rect renderBounds, {
double pixelRatio = 1.0, double pixelRatio = 1.0,
bool debugPaint = false, bool debugPaint = false,
}) { }) async {
RenderObject repaintBoundary = renderObject; RenderObject repaintBoundary = renderObject;
while (!repaintBoundary.isRepaintBoundary) { while (!repaintBoundary.isRepaintBoundary) {
repaintBoundary = repaintBoundary.parent!; repaintBoundary = repaintBoundary.parent!;
...@@ -604,7 +610,15 @@ class _ScreenshotPaintingContext extends PaintingContext { ...@@ -604,7 +610,15 @@ class _ScreenshotPaintingContext extends PaintingContext {
// been called successfully for all layers in the regular scene. // been called successfully for all layers in the regular scene.
repaintBoundary.debugLayer!.buildScene(ui.SceneBuilder()); 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 { ...@@ -1285,6 +1299,7 @@ mixin WidgetInspectorService {
return <String, Object?>{'result': null}; return <String, Object?>{'result': null};
} }
final ByteData? byteData = await image.toByteData(format:ui.ImageByteFormat.png); final ByteData? byteData = await image.toByteData(format:ui.ImageByteFormat.png);
image.dispose();
return <String, Object>{ return <String, Object>{
'result': base64.encoder.convert(Uint8List.view(byteData!.buffer)), 'result': base64.encoder.convert(Uint8List.view(byteData!.buffer)),
...@@ -3141,7 +3156,7 @@ class _InspectorOverlayLayer extends Layer { ...@@ -3141,7 +3156,7 @@ class _InspectorOverlayLayer extends Layer {
_InspectorOverlayRenderState? _lastState; _InspectorOverlayRenderState? _lastState;
/// Picture generated from _lastState. /// Picture generated from _lastState.
late ui.Picture _picture; ui.Picture? _picture;
TextPainter? _textPainter; TextPainter? _textPainter;
double? _textPainterMaxWidth; double? _textPainterMaxWidth;
...@@ -3150,6 +3165,7 @@ class _InspectorOverlayLayer extends Layer { ...@@ -3150,6 +3165,7 @@ class _InspectorOverlayLayer extends Layer {
void dispose() { void dispose() {
_textPainter?.dispose(); _textPainter?.dispose();
_textPainter = null; _textPainter = null;
_picture?.dispose();
super.dispose(); super.dispose();
} }
...@@ -3184,9 +3200,10 @@ class _InspectorOverlayLayer extends Layer { ...@@ -3184,9 +3200,10 @@ class _InspectorOverlayLayer extends Layer {
if (state != _lastState) { if (state != _lastState) {
_lastState = state; _lastState = state;
_picture?.dispose();
_picture = _buildPicture(state); _picture = _buildPicture(state);
} }
builder.addPicture(Offset.zero, _picture); builder.addPicture(Offset.zero, _picture!);
} }
ui.Picture _buildPicture(_InspectorOverlayRenderState state) { ui.Picture _buildPicture(_InspectorOverlayRenderState state) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment