Skip to content

Commit

Permalink
Avoid setting the Glide singleton when it's half initialized
Browse files Browse the repository at this point in the history
If we set the singleton before calling registerComponents, other threads
that call get() while registerComponents is being called may receive a
half initialized Glide object. Half initialized Glide objects may be
missing critical components and using them in other threads may break
lots of things.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=161232290
  • Loading branch information
sjudd committed Jul 13, 2017
1 parent 811f301 commit 6c56e38
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public class Glide implements ComponentCallbacks2 {
private static final String DEFAULT_DISK_CACHE_DIR = "image_manager_disk_cache";
private static final String TAG = "Glide";
private static volatile Glide glide;
private static volatile boolean isInitializing;

private final Engine engine;
private final BitmapPool bitmapPool;
Expand Down Expand Up @@ -151,7 +152,15 @@ public static Glide get(Context context) {
if (glide == null) {
synchronized (Glide.class) {
if (glide == null) {
// In the thread running initGlide(), one or more classes may call Glide.get(context).
// Without this check, those calls could trigger infinite recursion.
if (isInitializing) {
throw new IllegalStateException("You cannot call Glide.get() in registerComponents(),"
+ " use the provided Glide instance instead");
}
isInitializing = true;
initGlide(context);
isInitializing = false;
}
}
}
Expand All @@ -160,12 +169,12 @@ public static Glide get(Context context) {
}

@VisibleForTesting
public static void init(Glide glide) {
public static synchronized void init(Glide glide) {
Glide.glide = glide;
}

@VisibleForTesting
public static void tearDown() {
public static synchronized void tearDown() {
glide = null;
}

Expand Down Expand Up @@ -212,13 +221,14 @@ private static void initGlide(Context context) {
if (annotationGeneratedModule != null) {
annotationGeneratedModule.applyOptions(applicationContext, builder);
}
glide = builder.build(applicationContext);
Glide glide = builder.build(applicationContext);
for (GlideModule module : manifestModules) {
module.registerComponents(applicationContext, glide, glide.registry);
}
if (annotationGeneratedModule != null) {
annotationGeneratedModule.registerComponents(applicationContext, glide, glide.registry);
}
Glide.glide = glide;
}

@Nullable
Expand Down

0 comments on commit 6c56e38

Please sign in to comment.