@@ -307,22 +307,30 @@ private void assertNotCallingCallbacks() {
307
307
* @see #cancel()
308
308
*/
309
309
@ Override
310
- public synchronized void clear () {
311
- assertNotCallingCallbacks ();
312
- stateVerifier .throwIfRecycled ();
313
- if (status == Status .CLEARED ) {
314
- return ;
315
- }
316
- cancel ();
317
- // Resource must be released before canNotifyStatusChanged is called.
318
- if (resource != null ) {
319
- releaseResource (resource );
320
- }
321
- if (canNotifyCleared ()) {
322
- target .onLoadCleared (getPlaceholderDrawable ());
310
+ public void clear () {
311
+ Resource <R > toRelease = null ;
312
+ synchronized (this ) {
313
+ assertNotCallingCallbacks ();
314
+ stateVerifier .throwIfRecycled ();
315
+ if (status == Status .CLEARED ) {
316
+ return ;
317
+ }
318
+ cancel ();
319
+ // Resource must be released before canNotifyStatusChanged is called.
320
+ if (resource != null ) {
321
+ toRelease = resource ;
322
+ resource = null ;
323
+ }
324
+ if (canNotifyCleared ()) {
325
+ target .onLoadCleared (getPlaceholderDrawable ());
326
+ }
327
+
328
+ status = Status .CLEARED ;
323
329
}
324
330
325
- status = Status .CLEARED ;
331
+ if (toRelease != null ) {
332
+ engine .release (toRelease );
333
+ }
326
334
}
327
335
328
336
@ Override
@@ -332,11 +340,6 @@ public synchronized void pause() {
332
340
}
333
341
}
334
342
335
- private void releaseResource (Resource <?> resource ) {
336
- engine .release (resource );
337
- this .resource = null ;
338
- }
339
-
340
343
@ Override
341
344
public synchronized boolean isRunning () {
342
345
return status == Status .RUNNING || status == Status .WAITING_FOR_SIZE ;
@@ -506,53 +509,64 @@ private void notifyLoadFailed() {
506
509
@ SuppressWarnings ("unchecked" )
507
510
@ Override
508
511
public synchronized void onResourceReady (Resource <?> resource , DataSource dataSource ) {
509
- stateVerifier .throwIfRecycled ();
510
- loadStatus = null ;
511
- if (resource == null ) {
512
- GlideException exception =
513
- new GlideException (
514
- "Expected to receive a Resource<R> with an "
515
- + "object of "
516
- + transcodeClass
517
- + " inside, but instead got null." );
518
- onLoadFailed (exception );
519
- return ;
520
- }
512
+ Resource <?> toRelease = null ;
513
+ try {
514
+ synchronized (this ) {
515
+ stateVerifier .throwIfRecycled ();
516
+ loadStatus = null ;
517
+ if (resource == null ) {
518
+ GlideException exception =
519
+ new GlideException (
520
+ "Expected to receive a Resource<R> with an "
521
+ + "object of "
522
+ + transcodeClass
523
+ + " inside, but instead got null." );
524
+ onLoadFailed (exception );
525
+ return ;
526
+ }
521
527
522
- Object received = resource .get ();
523
- if (received == null || !transcodeClass .isAssignableFrom (received .getClass ())) {
524
- releaseResource (resource );
525
- GlideException exception =
526
- new GlideException (
527
- "Expected to receive an object of "
528
- + transcodeClass
529
- + " but instead"
530
- + " got "
531
- + (received != null ? received .getClass () : "" )
532
- + "{"
533
- + received
534
- + "} inside"
535
- + " "
536
- + "Resource{"
537
- + resource
538
- + "}."
539
- + (received != null
540
- ? ""
541
- : " "
542
- + "To indicate failure return a null Resource "
543
- + "object, rather than a Resource object containing null data." ));
544
- onLoadFailed (exception );
545
- return ;
546
- }
528
+ Object received = resource .get ();
529
+ if (received == null || !transcodeClass .isAssignableFrom (received .getClass ())) {
530
+ toRelease = resource ;
531
+ this .resource = null ;
532
+ GlideException exception =
533
+ new GlideException (
534
+ "Expected to receive an object of "
535
+ + transcodeClass
536
+ + " but instead"
537
+ + " got "
538
+ + (received != null ? received .getClass () : "" )
539
+ + "{"
540
+ + received
541
+ + "} inside"
542
+ + " "
543
+ + "Resource{"
544
+ + resource
545
+ + "}."
546
+ + (received != null
547
+ ? ""
548
+ : " "
549
+ + "To indicate failure return a null Resource "
550
+ + "object, rather than a Resource object containing null data." ));
551
+ onLoadFailed (exception );
552
+ return ;
553
+ }
547
554
548
- if (!canSetResource ()) {
549
- releaseResource (resource );
550
- // We can't put the status to complete before asking canSetResource().
551
- status = Status .COMPLETE ;
552
- return ;
553
- }
555
+ if (!canSetResource ()) {
556
+ toRelease = resource ;
557
+ this .resource = null ;
558
+ // We can't put the status to complete before asking canSetResource().
559
+ status = Status .COMPLETE ;
560
+ return ;
561
+ }
554
562
555
- onResourceReady ((Resource <R >) resource , (R ) received , dataSource );
563
+ onResourceReady ((Resource <R >) resource , (R ) received , dataSource );
564
+ }
565
+ } finally {
566
+ if (toRelease != null ) {
567
+ engine .release (toRelease );
568
+ }
569
+ }
556
570
}
557
571
558
572
/**
0 commit comments