Commit 1e0b613
Fix Glide IllegalArgumentException in MauiCustomTarget.clear() for destroyed activities (#29780)
## Problem
Random crashes occurring on Android devices with the error:
```
java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(RequestManagerRetriever.java:236)
at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:110)
at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:92)
at com.bumptech.glide.Glide.with(Glide.java:545)
at com.microsoft.maui.glide.MauiCustomTarget.lambda$clear$0$com-microsoft-maui-glide-MauiCustomTarget(MauiCustomTarget.java:88)
```
This happens when Glide attempts to clear image targets using a Context
whose underlying Activity has been destroyed.
## Solution
Added defensive context destruction check in `MauiCustomTarget.clear()`
before calling `Glide.with(context).clear(this)`.
**Key Changes:**
1. **Added private context lifecycle validation methods to
MauiCustomTarget:**
- `isContextDestroyed()` - Checks if context/activity is destroyed or
finishing
- `getActivity()` - Safely extracts Activity from Context (handles
ContextWrapper chains)
2. **Protected MauiCustomTarget.clear() operation:**
- Added context check before calling `Glide.with(context).clear(this)`
- Includes verbose logging when the clear operation is skipped due to
destroyed context
**Behavior:**
- When context is destroyed, `clear()` returns early without attempting
to call Glide
- Matches Glide's own validation logic: `activity.isFinishing() ||
activity.isDestroyed()`
- Logs a debug message when clear is skipped (if verbose logging is
enabled)
**Example of the fix:**
```java
private void clear() {
post(() -> {
if (isContextDestroyed(context)) {
if (logger.isVerboseLoggable) {
logger.v("clear() skipped - context destroyed: " + resourceLogIdentifier);
}
return;
}
Glide
.with(context)
.clear(this);
});
}
```
This is a surgical, minimal fix that targets the specific crash location
identified in the stack trace while maintaining all existing
functionality.
Fixes #29699.
---
💡 You can make Copilot smarter by setting up custom instructions,
customizing its development environment and configuring Model Context
Protocol (MCP) servers. Learn more <a
href="https://gh.io/copilot-coding-agent-tips">Copilot coding agent
tips</a> in the docs.
<!-- START COPILOT CODING AGENT TIPS -->
---
✨ Let Copilot coding agent [set things up for
you](https://github.com/dotnet/maui/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>1 parent a48cccb commit 1e0b613
1 file changed
Lines changed: 32 additions & 0 deletions
Lines changed: 32 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
| |||
80 | 82 | | |
81 | 83 | | |
82 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
83 | 109 | | |
84 | 110 | | |
85 | 111 | | |
86 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
87 | 119 | | |
88 | 120 | | |
89 | 121 | | |
| |||
0 commit comments