Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible NPE when Disk Cache fails #374

Closed
TWiStErRob opened this issue Mar 22, 2015 · 0 comments
Closed

Possible NPE when Disk Cache fails #374

TWiStErRob opened this issue Mar 22, 2015 · 0 comments
Labels
Milestone

Comments

@TWiStErRob
Copy link
Collaborator

Glide Version/Integration library (if any): 3.5.2
Device/Android Version: N/A
Issue details/Repro steps:

DiskCache.Factory.build() method JavaDoc says

Returns a new disk cache, or {@code null} if no disk cache could be created.

and Engine.LazyDiskCacheProvider.getDiskCache() just returns the value,
then DecodeJob uses that return value in a chain: diskCacheProvider.getDiskCache().get|put|delete().

Meaning a custom Disk Cache Factory may fail:

public static class GlideSetup implements GlideModule {
    @Override public void applyOptions(final Context context, GlideBuilder builder) {
        builder.setDiskCache(new DiskCache.Factory() {
            @Override public DiskCache build() {
                File cacheDir = Paths.getGlideCacheDirectory(context);
                if (cacheDir == null
                        || (!cacheDir.mkdirs() && (!cacheDir.exists() || !cacheDir.isDirectory()))) {
                    // Note that the condition is almost the same as Glide.getPhotoCacheDir
                    return null;
                }
                return DiskLruCacheWrapper.get(cacheDir, 250 * 1024 * 1024);
            }
        });
    }
    @Override public void registerComponents(Context context, Glide glide) {}
}

In case the DiskCache.Factory can't create a cache (no available directory, the a App will crash with an NPE in Glide, however it could probably work just fine without a disk cache, maybe a little slower.

Probably the

        if (diskCache == null) {
            diskCache = new DiskCacheAdapter();
        }

lines in InternalCacheDiskCacheFactory.build should be moved to LazyDiskCacheProvider, so the fallback applies to all caches, not just the Glide built-in one.

PS: Also note that's an awful lot of lines, just to move the disk cache to another directory.

@sjudd sjudd added the bug label Mar 23, 2015
@sjudd sjudd added this to the 3.6.0 milestone Mar 24, 2015
@sjudd sjudd closed this as completed in c630313 Mar 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants