bumptech / bumptech/glide

Why does SizeConfigStrategy.decrementBitmapOfSize throws NPE instead of giving a warning and what am i doing wrong?

Open
#2,735 9 comments 0 reactions 0 assignees View on GitHub
bug repro-needed
Dominant language
Java
Stars
35k
Forks
6.2k
Avg merge
1d 11h
Merged PRs (30d)
8

Description

**Glide Version**: 4.4

**Integration libraries**: none

**Device/Android Version**: Completely random

**Issue details / Repro steps / Use case background**:
Hi there. First of all, i have been searching in issues / documentation /web for finding an answer to this question for days (I've also asked a question in SO but sadly, no one answered)
While Glide is an amazing library, it is also a bit overwhelming library at least for me to understand in such a short time.

In the project that i inherited, the app's Fabric.io dashboard flooded with NPE errors from SizeConfigStrategy.decrementBitmapOfSize. I cannot replicate this error in my test devices, (neighter our other testers) but yet this the top error in our fabric dashboard. When i check the source code i see that SizeConfigStrategy. decrementBitmapOfSize function throws this exception when it couldn't find a match. But why doesn't glide shows a warning about this error, and not throw an exception or why can't we catch the source of this exception?

I have also implemented a Glide Module with;

```java
@com.bumptech.glide.annotation.GlideModule
public class GlideModule extends AppGlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
super.applyOptions(context, builder);
Timber.i("Configuring Glide Module!");
builder.setDiskCacheExecutor(GlideExecutor.newDiskCacheExecutor(GlideExecutor.UncaughtThrowableStrategy.IGNORE));
builder.setSourceExecutor(GlideExecutor.newSourceExecutor(GlideExecutor.UncaughtThrowableStrategy.IGNORE));
}

@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
super.registerComponents(context, glide, registry);
}
}
```

and i see that module is running, but Glide doesn't seem to catching this exception.

And this is the static function found in the project for loading images into imageviews:

```java
private static BitmapImageViewTarget loadCircleImageIntoView(RequestManager manager, final Context context, final ImageView imageView, String imageUrl, @DrawableRes int placeholder, boolean fitCenter, boolean centerCrop) {

String cacheSignature = imageUrl+"-"+fitCenter+"-"+centerCrop;
RequestBuilder requestBuilder = Glide
.with(context)
.asBitmap();
RequestOptions options = new RequestOptions()
.circleCrop()
.fitCenter()
.centerInside()
.signature(new ObjectKey(cacheSignature))
.diskCacheStrategy(DiskCacheStrategy.ALL);

if (fitCenter){
options = options.fitCenter();
}
if (centerCrop){
options = options.centerCrop();
}
requestBuilder = requestBuilder.apply(options);
return requestBuilder
.load(imageUrl)
.into(new BitmapImageViewTarget(imageView){
@Override
protected void setResource(Bitmap resource) {
super.setResource(resource);
RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCircular(true);
imageView.setImageDrawable(circularBitmapDrawable);
}
});
}
```

So my questions are:
1- How can we "catch" this error?
2- Why does "GlideExecutor.UncaughtThrowableStrategy.IGNORE" doesn't work in this scenario
3- Is it really necessary to throw an error in this situation? (Don't get me wrong, i am just trting to understand how glide works)

**Layout XML**: -

**Stack trace / LogCat**:
```ruby
Fatal Exception: java.lang.NullPointerException: Tried to decrement empty size, size: 108732, removed: [108732](ARGB_8888), this: SizeConfigStrategy{groupedMap=GroupedLinkedMap( {[4665600](ARGB_8888):3}, {[46656](ARGB_8888):4}, {[577600](ARGB_8888):0}, {[47524](ARGB_8888):1}, {[390640](ARGB_8888):1}, {[108732](ARGB_8888):0} ), sortedSizes=(null[{}], ARGB_8888[{46656=1, 47524=1, 390640=1, 4665600=1}])}
at com.bumptech.glide.load.engine.bitmap_recycle.SizeConfigStrategy.decrementBitmapOfSize(SizeConfigStrategy.java:111)
at com.bumptech.glide.load.engine.bitmap_recycle.SizeConfigStrategy.removeLast(SizeConfigStrategy.java:99)
at com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool.trimToSize(LruBitmapPool.java:223)
at com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool.clearMemory(LruBitmapPool.java:205)
at com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool.trimMemory(LruBitmapPool.java:215)
at com.bumptech.glide.Glide.trimMemory(Glide.java:556)
at com.bumptech.glide.Glide.onTrimMemory(Glide.java:752)
at android.app.Application.onTrimMemory(Application.java:136)
at android.app.ActivityThread.handleTrimMemory(ActivityThread.java:5229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1765)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.