From adc0a37ea39781076e6c1f3dd48f6c8d0728f3f9 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Mon, 6 Jun 2022 17:23:04 -0700 Subject: [PATCH] Return an error if a SingleFrameCodec's cached decoded image has been disposed (flutter/engine#33819) --- .../flutter/lib/ui/painting/single_frame_codec.cc | 3 +++ engine/src/flutter/testing/dart/codec_test.dart | 14 ++++++++++++++ .../flutter/testing/dart/image_dispose_test.dart | 12 ------------ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/engine/src/flutter/lib/ui/painting/single_frame_codec.cc b/engine/src/flutter/lib/ui/painting/single_frame_codec.cc index 0298215767..ce1f6fc91c 100644 --- a/engine/src/flutter/lib/ui/painting/single_frame_codec.cc +++ b/engine/src/flutter/lib/ui/painting/single_frame_codec.cc @@ -33,6 +33,9 @@ Dart_Handle SingleFrameCodec::getNextFrame(Dart_Handle callback_handle) { } if (status_ == Status::kComplete) { + if (!cached_image_->image()) { + return tonic::ToDart("Decoded image has been disposed"); + } tonic::DartInvoke(callback_handle, {tonic::ToDart(cached_image_), tonic::ToDart(0)}); return Dart_Null(); diff --git a/engine/src/flutter/testing/dart/codec_test.dart b/engine/src/flutter/testing/dart/codec_test.dart index 86ce31a8d6..f06a5e4134 100644 --- a/engine/src/flutter/testing/dart/codec_test.dart +++ b/engine/src/flutter/testing/dart/codec_test.dart @@ -85,6 +85,20 @@ void main() { [0, 240, 246], ])); }); + + test('disposed decoded image', () async { + Uint8List data = await _getSkiaResource('flutter_logo.jpg').readAsBytes(); + final ui.Codec codec = await ui.instantiateImageCodec(data); + final ui.FrameInfo frameInfo = await codec.getNextFrame(); + expect(frameInfo.image, isNotNull); + frameInfo.image.dispose(); + try { + await codec.getNextFrame(); + fail('exception not thrown'); + } on Exception catch (e) { + expect(e.toString(), contains('Decoded image has been disposed')); + } + }); } /// Returns a File handle to a file in the skia/resources directory. diff --git a/engine/src/flutter/testing/dart/image_dispose_test.dart b/engine/src/flutter/testing/dart/image_dispose_test.dart index d1c1d308cd..fe0d11fda8 100644 --- a/engine/src/flutter/testing/dart/image_dispose_test.dart +++ b/engine/src/flutter/testing/dart/image_dispose_test.dart @@ -114,18 +114,6 @@ void main() { expect(frame2.image.isCloneOf(frame.image), false); }); - test('getNextFrame does not return a disposed image', () async { - final Uint8List bytes = await readFile('2x2.png'); - final Codec codec = await instantiateImageCodec(bytes); - final FrameInfo frame = await codec.getNextFrame(); - - frame.image.dispose(); - - final FrameInfo frame2 = await codec.getNextFrame(); - expect(frame2.image.clone()..dispose(), isNotNull); - frame2.image.dispose(); - }); - test('debugDisposed works', () async { final Uint8List bytes = await readFile('2x2.png'); final Codec codec = await instantiateImageCodec(bytes);