dispose images correctly

This commit is contained in:
Fabian Freund
2026-01-10 06:28:26 +01:00
parent 9a04b2d89a
commit f84b8838e1
10 changed files with 120 additions and 23 deletions
+20 -2
View File
@@ -20,10 +20,28 @@
import 'dart:ui';
class EquatableImage {
final Image value;
Image? _value;
final int _imageHash;
bool _isDisposed = false;
EquatableImage(this.value, {required int hash}) : _imageHash = hash;
EquatableImage(Image value, {required int hash})
: _value = value,
_imageHash = hash;
/// The underlying ui.Image. Returns null if disposed.
Image? get value => _isDisposed ? null : _value;
/// Whether this image has been disposed.
bool get isDisposed => _isDisposed;
/// Disposes the underlying ui.Image to free GPU memory.
/// This is safe to call multiple times.
void dispose() {
if (_isDisposed) return;
_isDisposed = true;
_value?.dispose();
_value = null;
}
@override
int get hashCode => _imageHash.hashCode;
+5 -2
View File
@@ -86,11 +86,14 @@ class GenericWebsiteService extends _$GenericWebsiteService {
//Global icon cache
late CacheRepository _cacheRepository;
//Local decoded icon cache
final LRUCache<String, BrowserIcon> _browserIconCache;
late final LRUCache<String, BrowserIcon> _browserIconCache;
GenericWebsiteService()
: _iconsService = GeckoIconService(),
_browserIconCache = LRUCache(50);
_browserIconCache = LRUCache(
50,
onEvict: (icon) => icon.image.dispose(),
);
@override
void build() {
@@ -42,7 +42,7 @@ final class GenericWebsiteServiceProvider
}
String _$genericWebsiteServiceHash() =>
r'44d6e4cc161b1200070d99e29c1db04cd18ca091';
r'32a561820bda432f3c17e05b9da9c85871efffb4';
abstract class _$GenericWebsiteService extends $Notifier<void> {
void build();