Baseflow / flutter_cached_network_image

Download, cache and show images in a flutter app
https://baseflow.com
2.44k stars 654 forks source link

Decoded image has been disposed #805

Open thiagocarvalhodev opened 1 year ago

thiagocarvalhodev commented 1 year ago

🐛 Bug Report

Expected behavior

Should load the image correctly.

Reproduction steps

I'm checking how much memory is currently in the cache and then cleaning the cache when full.

I'm receiving the following logs on Crashlytics:

Non-fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: Exception: Decoded image has been disposed. Error thrown null.
       at FirebaseCrashlytics.recordError(firebase_crashlytics.dart:119)
       at CustomExtendedImage.build.<fn>(custom_extended_image.dart:72)
       at CachedNetworkImage._octoErrorBuilder(cached_image_widget.dart:318)

Check the code:

void _checkMemory() {
    late int maxBytes;
    final ConfigProvider configProvider = locator<ConfigProvider>();
    if (Platform.isAndroid) {
      maxBytes = 1024 * 1024 * 100 * configProvider.androidMaxCache; // 500 MB
    } else {
      maxBytes = 1024 * 1024 * 100 * configProvider.iOSMaxCache; // 400 MB
    }
    FirebaseCrashlytics.instance
        .log('Max bytes in cache for images: ${maxBytes.toString()}');

    ImageCache _imageCache = PaintingBinding.instance.imageCache;
    if (_imageCache.currentSize >= 400 ||
        _imageCache.currentSizeBytes >= maxBytes) {
      FirebaseCrashlytics.instance.log(
          'Current image cache size: ${_imageCache.currentSizeBytes}. Its greater than the max: ${maxBytes.toString()} or greater than 400mb.\nCleaning images from cache.');

      _imageCache.clearLiveImages();
      _imageCache.clear();

      FirebaseCrashlytics.instance.log('Images removed from cache.');
      return;
    }
  }

  @override
  Widget build(BuildContext context) {
    final int? cacheWidthInt =
        cacheWidth ? MediaQuery.of(context).size.width.toInt() : null;
    final int? cacheHeightInt =
        cacheHeight ? MediaQuery.of(context).size.height.toInt() : null;
    _checkMemory();
    return CachedNetworkImage(
      imageUrl: url,
      fit: BoxFit.cover,
      height: height ?? MediaQuery.of(context).size.height,
      width: width ?? MediaQuery.of(context).size.width,
      placeholderFadeInDuration: Duration.zero,
      fadeInDuration: Duration.zero,
      fadeOutDuration: Duration.zero,
      errorWidget: (context, url, error) {
        FirebaseCrashlytics.instance.log(
          'An error occur loading image: $url\nShowing error placeholder.',
        );

        FirebaseCrashlytics.instance.recordError(error, null);

        return const Icon(Icons.error);
      },
      memCacheWidth: cacheWidth ? (cacheWidthInt! * 1.5).toInt() : null,
      color: color,
      filterQuality: FilterQuality.medium,
      memCacheHeight: cacheHeight ? (cacheHeightInt! * 1.4).toInt() : null,
    );
}

Configuration

Version: 3.2.3

Platform:

mohachouch commented 1 year ago

Hello, I have the same error in android : Exception: Decoded image has been disposed.

flutter: 3.3.10 cached_network_image : 3.2.3

#0      Codec.getNextFrame (dart:ui/painting.dart:2002:7)
#1      MultiImageStreamCompleter._decodeNextFrameAndSchedule (package:cached_network_image/src/image_provider/multi_image_stream_completer.dart:127:34)
#2      MultiImageStreamCompleter.addListener (package:cached_network_image/src/image_provider/multi_image_stream_completer.dart:170:42)
#3      ImageStream.addListener (package:flutter/src/painting/image_stream.dart:373:26)
#4      _ImageState._listenToStream (package:flutter/src/widgets/image.dart:1209:19)
#5      _ImageState.didChangeDependencies (package:flutter/src/widgets/image.dart:1068:7)

Thanks for your help

ggirotto commented 1 year ago

I'm facing the same issue. Not sure how to reproduce but I'm seeing some reports on the crashlytics tool we use

NSLogZfRee commented 10 months ago

flutter 3.10 cached_network_image 3.3.0

Non-fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: Exception: Decoded image has been disposed. Error thrown resolving an image frame. at Codec.getNextFrame(dart:ui) at MultiImageStreamCompleter._decodeNextFrameAndSchedule(multi_image_stream_completer.dart:127) at MultiImageStreamCompleter.addListener(multi_image_stream_completer.dart:170) at ImageStream.addListener(image_stream.dart:376)

SmartQ-Rohit commented 1 month ago

I also getting same issue in cached_network_image: ^3.4.1 library,Fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: Exception: Decoded image has been disposed. Error thrown . at _NativeCodec.getNextFrame(dart:ui) at MultiImageStreamCompleter._decodeNextFrameAndSchedule(multi_image_stream_completer.dart:133) at MultiImageStreamCompleter.addListener(multi_image_stream_completer.dart:176) at ImageStream.addListener(image_stream.dart:402) at _DecorationImagePainter.paint(decoration_image.dart:371) at _BoxDecorationPainter._paintBackgroundImage(box_decoration.dart:488)