Skip to content

Commit 83da7df

Browse files
romtsnclaude
andcommitted
fix(screenshot): Recycle bitmap copy on masking failure to prevent memory leak
When an exception occurred in applyMasking after creating a mutable copy of the bitmap, the catch block returned the original screenshot without recycling the copy. This caused bitmap memory to accumulate until GC runs, potentially causing OOM issues on frequent errors. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 115107b commit 83da7df

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/ScreenshotEventProcessor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,16 @@ private boolean isMaskingEnabled() {
148148

149149
private @NotNull Bitmap applyMasking(
150150
final @NotNull Bitmap screenshot, final @NotNull View rootView) {
151+
Bitmap mutableBitmap = screenshot;
152+
boolean createdCopy = false;
151153
try {
152154
// Make bitmap mutable if needed
153-
Bitmap mutableBitmap = screenshot;
154155
if (!screenshot.isMutable()) {
155156
mutableBitmap = screenshot.copy(Bitmap.Config.ARGB_8888, true);
156157
if (mutableBitmap == null) {
157158
return screenshot;
158159
}
160+
createdCopy = true;
159161
}
160162

161163
// we can access it here, since it's "internal" only for Kotlin
@@ -170,13 +172,17 @@ private boolean isMaskingEnabled() {
170172
}
171173

172174
// Recycle original if we created a copy
173-
if (mutableBitmap != screenshot && !screenshot.isRecycled()) {
175+
if (createdCopy && !screenshot.isRecycled()) {
174176
screenshot.recycle();
175177
}
176178

177179
return mutableBitmap;
178180
} catch (Throwable e) {
179181
options.getLogger().log(SentryLevel.ERROR, "Failed to mask screenshot", e);
182+
// Recycle the copy if we created one, to avoid memory leak
183+
if (createdCopy && !mutableBitmap.isRecycled()) {
184+
mutableBitmap.recycle();
185+
}
180186
return screenshot;
181187
}
182188
}

0 commit comments

Comments
 (0)