fix: prevent GL texture leak when tile creation fails mid-array

If Bitmap.createBitmap() or loadTexture() threw (e.g. OutOfMemoryError)
while building the GLPicture tiles Array, any textures already uploaded
to the GPU in previous iterations were abandoned because the tiles field
was never assigned and recycle() could not be called. Switched from the
Array{} lambda (which only exposes partial state through the exception)
to an explicit loop that tracks created tiles in a List, and added a
catch-rethrow that deletes all partial textures before propagating.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Anthonyy232andClaude Sonnet 4.6 committed 2026-03-28 14:05:24 -07:00
1 parent 34031fe89d
commit 73ca207301
1 file changed
+50 -40
@@ -63,54 +63,64 @@ class GLPicture(
Log.d(TAG, "Creating GLPicture: ${width}x${height}, tiling to ${cols}x${rows} (tile size: $tileSize)") Log.d(TAG, "Creating GLPicture: ${width}x${height}, tiling to ${cols}x${rows} (tile size: $tileSize)")
tiles = Array(cols * rows) { index -> val partialTiles = ArrayList<Tile>(cols * rows)
val col = index % cols try {
val row = index / cols for (index in 0 until cols * rows) {
val col = index % cols
val row = index / cols
val tileX = col * tileSize val tileX = col * tileSize
val tileY = row * tileSize val tileY = row * tileSize
val tileWidth = min(tileSize, width - tileX) val tileWidth = min(tileSize, width - tileX)
val tileHeight = min(tileSize, height - tileY) val tileHeight = min(tileSize, height - tileY)
// Calculate geometry once at creation time (not per-frame) // Calculate geometry once at creation time (not per-frame)
val left = -1f + (tileX.toFloat() / width) * 2f val left = -1f + (tileX.toFloat() / width) * 2f
val right = -1f + ((tileX + tileWidth).toFloat() / width) * 2f val right = -1f + ((tileX + tileWidth).toFloat() / width) * 2f
val bottom = 1f - ((tileY + tileHeight).toFloat() / height) * 2f val bottom = 1f - ((tileY + tileHeight).toFloat() / height) * 2f
val top = 1f - (tileY.toFloat() / height) * 2f val top = 1f - (tileY.toFloat() / height) * 2f
val vertices = floatArrayOf( val vertices = floatArrayOf(
left, bottom, // Bottom-left left, bottom, // Bottom-left
right, bottom, // Bottom-right right, bottom, // Bottom-right
left, top, // Top-left left, top, // Top-left
right, top // Top-right right, top // Top-right
) )
val texCoords = floatArrayOf( val texCoords = floatArrayOf(
0f, 1f, // Bottom-left 0f, 1f, // Bottom-left
1f, 1f, // Bottom-right 1f, 1f, // Bottom-right
0f, 0f, // Top-left 0f, 0f, // Top-left
1f, 0f // Top-right 1f, 0f // Top-right
) )
// Pre-allocate buffers (reused every frame) // Pre-allocate buffers (reused every frame)
val vertexBuffer = createFloatBuffer(vertices) val vertexBuffer = createFloatBuffer(vertices)
val texCoordBuffer = createFloatBuffer(texCoords) val texCoordBuffer = createFloatBuffer(texCoords)
// Extract and upload tile bitmap // Extract and upload tile bitmap
val tileBitmap = Bitmap.createBitmap(bitmap, tileX, tileY, tileWidth, tileHeight) val tileBitmap = Bitmap.createBitmap(bitmap, tileX, tileY, tileWidth, tileHeight)
val textureId = loadTexture(tileBitmap) val textureId = loadTexture(tileBitmap)
tileBitmap.recycle() tileBitmap.recycle()
Tile( partialTiles.add(Tile(
textureId = textureId, textureId = textureId,
x = tileX, x = tileX,
y = tileY, y = tileY,
width = tileWidth, width = tileWidth,
height = tileHeight, height = tileHeight,
vertexBuffer = vertexBuffer, vertexBuffer = vertexBuffer,
texCoordBuffer = texCoordBuffer texCoordBuffer = texCoordBuffer
) ))
}
} catch (e: Throwable) {
// Recycle any textures already uploaded to the GPU before re-throwing
for (tile in partialTiles) {
GLUtil.deleteTexture(tile.textureId)
}
throw e
} }
tiles = partialTiles.toTypedArray()
} }
/** /**