Unverified Commit 37de4960 authored by Dan Field's avatar Dan Field Committed by GitHub

Remove listeners from pending images when clearing cache (#107276)

parent 742a1c38
...@@ -195,6 +195,9 @@ class ImageCache { ...@@ -195,6 +195,9 @@ class ImageCache {
image.dispose(); image.dispose();
} }
_cache.clear(); _cache.clear();
for (final _PendingImage pendingImage in _pendingImages.values) {
pendingImage.removeListener();
}
_pendingImages.clear(); _pendingImages.clear();
_currentSizeBytes = 0; _currentSizeBytes = 0;
} }
......
...@@ -179,6 +179,10 @@ void main() { ...@@ -179,6 +179,10 @@ void main() {
return completer1; return completer1;
})! as TestImageStreamCompleter; })! as TestImageStreamCompleter;
// Make the image seem live.
final ImageStreamListener listener = ImageStreamListener((_, __) {});
completer1.addListener(listener);
expect(imageCache.statusForKey(testImage).pending, true); expect(imageCache.statusForKey(testImage).pending, true);
expect(imageCache.statusForKey(testImage).live, true); expect(imageCache.statusForKey(testImage).live, true);
imageCache.clear(); imageCache.clear();
...@@ -346,10 +350,10 @@ void main() { ...@@ -346,10 +350,10 @@ void main() {
imageCache.clear(); imageCache.clear();
expect(imageCache.statusForKey(testImage1).pending, false); expect(imageCache.statusForKey(testImage1).pending, false);
expect(imageCache.statusForKey(testImage1).live, true); expect(imageCache.statusForKey(testImage1).live, false);
completer1.testSetImage(testImage1); completer1.testSetImage(testImage1);
expect(imageCache.statusForKey(testImage1).keepAlive, true); expect(imageCache.statusForKey(testImage1).keepAlive, false);
expect(imageCache.statusForKey(testImage1).live, false); expect(imageCache.statusForKey(testImage1).live, false);
imageCache.putIfAbsent(testImage2, () => completer2); imageCache.putIfAbsent(testImage2, () => completer2);
...@@ -602,4 +606,27 @@ void main() { ...@@ -602,4 +606,27 @@ void main() {
imageInfo.dispose(); imageInfo.dispose();
expect(testImage.debugGetOpenHandleStackTraces()!.length, 0); expect(testImage.debugGetOpenHandleStackTraces()!.length, 0);
}, skip: kIsWeb); // https://github.com/flutter/flutter/issues/87442 }, skip: kIsWeb); // https://github.com/flutter/flutter/issues/87442
test('clear does not leave pending images stuck', () async {
final ui.Image testImage = await createTestImage(width: 8, height: 8);
final TestImageStreamCompleter completer1 = TestImageStreamCompleter();
imageCache.putIfAbsent(testImage, () {
return completer1;
});
expect(imageCache.statusForKey(testImage).pending, true);
expect(imageCache.statusForKey(testImage).live, true);
expect(imageCache.statusForKey(testImage).keepAlive, false);
imageCache.clear();
// No one else is listening to the completer. It should not be considered
// live anymore.
expect(imageCache.statusForKey(testImage).pending, false);
expect(imageCache.statusForKey(testImage).live, false);
expect(imageCache.statusForKey(testImage).keepAlive, false);
});
} }
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