From 011332e50936274196aad32b983c2de2c7a27e07 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 11 Sep 2022 09:56:26 -0500 Subject: [PATCH] fix(mobile) memory leaked causes app to crash when swiping (#673) * Dispose image provider when swiping away from the asset --- .../asset_viewer/ui/remote_photo_view.dart | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/mobile/lib/modules/asset_viewer/ui/remote_photo_view.dart b/mobile/lib/modules/asset_viewer/ui/remote_photo_view.dart index b6133bc256..5cfcd2af29 100644 --- a/mobile/lib/modules/asset_viewer/ui/remote_photo_view.dart +++ b/mobile/lib/modules/asset_viewer/ui/remote_photo_view.dart @@ -12,6 +12,9 @@ class _RemotePhotoViewState extends State { bool _zoomedIn = false; static const int swipeThreshold = 100; + late CachedNetworkImageProvider fullProvider; + late CachedNetworkImageProvider previewProvider; + late CachedNetworkImageProvider thumbnailProvider; @override Widget build(BuildContext context) { @@ -65,7 +68,10 @@ class _RemotePhotoViewState extends State { } CachedNetworkImageProvider _authorizedImageProvider( - String url, String cacheKey, BaseCacheManager? cacheManager) { + String url, + String cacheKey, + BaseCacheManager? cacheManager, + ) { return CachedNetworkImageProvider( url, headers: {"Authorization": widget.authToken}, @@ -104,7 +110,7 @@ class _RemotePhotoViewState extends State { } void _loadImages() { - CachedNetworkImageProvider thumbnailProvider = _authorizedImageProvider( + thumbnailProvider = _authorizedImageProvider( widget.thumbnailUrl, widget.cacheKey, widget.thumbnailCacheManager, @@ -121,7 +127,7 @@ class _RemotePhotoViewState extends State { ); if (widget.previewUrl != null) { - CachedNetworkImageProvider previewProvider = _authorizedImageProvider( + previewProvider = _authorizedImageProvider( widget.previewUrl!, "${widget.cacheKey}_previewStage", widget.previewCacheManager, @@ -133,7 +139,7 @@ class _RemotePhotoViewState extends State { ); } - CachedNetworkImageProvider fullProvider = _authorizedImageProvider( + fullProvider = _authorizedImageProvider( widget.imageUrl, "${widget.cacheKey}_fullStage", widget.fullCacheManager, @@ -150,6 +156,19 @@ class _RemotePhotoViewState extends State { _loadImages(); super.initState(); } + + @override + void dispose() async { + super.dispose(); + await thumbnailProvider.evict(); + await fullProvider.evict(); + + if (widget.previewUrl != null) { + await previewProvider.evict(); + } + + _imageProvider.evict(); + } } class RemotePhotoView extends StatefulWidget {