Fix potential bitmap leaks in FrameTimingsObserver (#55652)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/55652

Address missing `recycle()` calls for bitmaps in frame timing screenshot capture on Android, which could leak either on exceptions or when window dimensions change.

Changelog: [Internal]

Reviewed By: rubennorte

Differential Revision: D93863311

fbshipit-source-id: 3180f1be4df5b1dec2742d93e38886fb4db4e4d0
This commit is contained in:
Alex Hunt authored and meta-codesync[bot] committed 2026-02-21 11:21:10 -08:00
1 parent 47c7165c3a
commit 3918dd1008
1 file changed
+12 -8
@@ -73,6 +73,7 @@ internal class FrameTimingsObserver(
if (it.width == width && it.height == height) {
it
} else {
it.recycle()
null
}
} ?: Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).also { bitmapBuffer = it }
@@ -83,26 +84,29 @@ internal class FrameTimingsObserver(
{ copyResult ->
if (copyResult == PixelCopy.SUCCESS) {
CoroutineScope(Dispatchers.Default).launch {
var scaledBitmap: Bitmap? = null
try {
val scaleFactor = 0.25f
val scaledWidth = (width * scaleFactor).toInt()
val scaledHeight = (height * scaleFactor).toInt()
val scaledBitmap =
Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true)
scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true)
val outputStream = ByteArrayOutputStream()
val compressFormat =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
Bitmap.CompressFormat.WEBP_LOSSY
else Bitmap.CompressFormat.WEBP
scaledBitmap.compress(compressFormat, 0, outputStream)
val bytes = outputStream.toByteArray()
val base64 = Base64.encodeToString(bytes, Base64.NO_WRAP)
continuation.resume(base64)
scaledBitmap.recycle()
val base64 =
ByteArrayOutputStream().use { outputStream ->
scaledBitmap.compress(compressFormat, 0, outputStream)
Base64.encodeToString(outputStream.toByteArray(), Base64.NO_WRAP)
}
continuation.resume(base64)
} catch (e: Exception) {
continuation.resume(null)
} finally {
scaledBitmap?.recycle()
}
}
} else {