Remove listeners from pending images when clearing cache (#107276)
This commit is contained in:
@@ -195,6 +195,9 @@ class ImageCache {
|
||||
image.dispose();
|
||||
}
|
||||
_cache.clear();
|
||||
for (final _PendingImage pendingImage in _pendingImages.values) {
|
||||
pendingImage.removeListener();
|
||||
}
|
||||
_pendingImages.clear();
|
||||
_currentSizeBytes = 0;
|
||||
}
|
||||
|
||||
@@ -179,6 +179,10 @@ void main() {
|
||||
return completer1;
|
||||
})! as TestImageStreamCompleter;
|
||||
|
||||
// Make the image seem live.
|
||||
final ImageStreamListener listener = ImageStreamListener((_, __) {});
|
||||
completer1.addListener(listener);
|
||||
|
||||
expect(imageCache.statusForKey(testImage).pending, true);
|
||||
expect(imageCache.statusForKey(testImage).live, true);
|
||||
imageCache.clear();
|
||||
@@ -346,10 +350,10 @@ void main() {
|
||||
|
||||
imageCache.clear();
|
||||
expect(imageCache.statusForKey(testImage1).pending, false);
|
||||
expect(imageCache.statusForKey(testImage1).live, true);
|
||||
expect(imageCache.statusForKey(testImage1).live, false);
|
||||
|
||||
completer1.testSetImage(testImage1);
|
||||
expect(imageCache.statusForKey(testImage1).keepAlive, true);
|
||||
expect(imageCache.statusForKey(testImage1).keepAlive, false);
|
||||
expect(imageCache.statusForKey(testImage1).live, false);
|
||||
|
||||
imageCache.putIfAbsent(testImage2, () => completer2);
|
||||
@@ -602,4 +606,27 @@ void main() {
|
||||
imageInfo.dispose();
|
||||
expect(testImage.debugGetOpenHandleStackTraces()!.length, 0);
|
||||
}, 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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user