From 73ca207301c86fbcded90ebe0ebd49498c62cb0e Mon Sep 17 00:00:00 2001 From: Anthonyy232 Date: Sat, 28 Mar 2026 14:05:24 -0700 Subject: [PATCH] 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 --- .../service/livewallpaper/gl/GLPicture.kt | 90 ++++++++++--------- 1 file changed, 50 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/com/anthonyla/paperize/service/livewallpaper/gl/GLPicture.kt b/app/src/main/java/com/anthonyla/paperize/service/livewallpaper/gl/GLPicture.kt index caeef53..0ce671e 100644 --- a/app/src/main/java/com/anthonyla/paperize/service/livewallpaper/gl/GLPicture.kt +++ b/app/src/main/java/com/anthonyla/paperize/service/livewallpaper/gl/GLPicture.kt @@ -63,54 +63,64 @@ class GLPicture( Log.d(TAG, "Creating GLPicture: ${width}x${height}, tiling to ${cols}x${rows} (tile size: $tileSize)") - tiles = Array(cols * rows) { index -> - val col = index % cols - val row = index / cols + val partialTiles = ArrayList(cols * rows) + try { + for (index in 0 until cols * rows) { + val col = index % cols + val row = index / cols - val tileX = col * tileSize - val tileY = row * tileSize - val tileWidth = min(tileSize, width - tileX) - val tileHeight = min(tileSize, height - tileY) + val tileX = col * tileSize + val tileY = row * tileSize + val tileWidth = min(tileSize, width - tileX) + val tileHeight = min(tileSize, height - tileY) - // Calculate geometry once at creation time (not per-frame) - val left = -1f + (tileX.toFloat() / width) * 2f - val right = -1f + ((tileX + tileWidth).toFloat() / width) * 2f - val bottom = 1f - ((tileY + tileHeight).toFloat() / height) * 2f - val top = 1f - (tileY.toFloat() / height) * 2f + // Calculate geometry once at creation time (not per-frame) + val left = -1f + (tileX.toFloat() / width) * 2f + val right = -1f + ((tileX + tileWidth).toFloat() / width) * 2f + val bottom = 1f - ((tileY + tileHeight).toFloat() / height) * 2f + val top = 1f - (tileY.toFloat() / height) * 2f - val vertices = floatArrayOf( - left, bottom, // Bottom-left - right, bottom, // Bottom-right - left, top, // Top-left - right, top // Top-right - ) + val vertices = floatArrayOf( + left, bottom, // Bottom-left + right, bottom, // Bottom-right + left, top, // Top-left + right, top // Top-right + ) - val texCoords = floatArrayOf( - 0f, 1f, // Bottom-left - 1f, 1f, // Bottom-right - 0f, 0f, // Top-left - 1f, 0f // Top-right - ) + val texCoords = floatArrayOf( + 0f, 1f, // Bottom-left + 1f, 1f, // Bottom-right + 0f, 0f, // Top-left + 1f, 0f // Top-right + ) - // Pre-allocate buffers (reused every frame) - val vertexBuffer = createFloatBuffer(vertices) - val texCoordBuffer = createFloatBuffer(texCoords) + // Pre-allocate buffers (reused every frame) + val vertexBuffer = createFloatBuffer(vertices) + val texCoordBuffer = createFloatBuffer(texCoords) - // Extract and upload tile bitmap - val tileBitmap = Bitmap.createBitmap(bitmap, tileX, tileY, tileWidth, tileHeight) - val textureId = loadTexture(tileBitmap) - tileBitmap.recycle() + // Extract and upload tile bitmap + val tileBitmap = Bitmap.createBitmap(bitmap, tileX, tileY, tileWidth, tileHeight) + val textureId = loadTexture(tileBitmap) + tileBitmap.recycle() - Tile( - textureId = textureId, - x = tileX, - y = tileY, - width = tileWidth, - height = tileHeight, - vertexBuffer = vertexBuffer, - texCoordBuffer = texCoordBuffer - ) + partialTiles.add(Tile( + textureId = textureId, + x = tileX, + y = tileY, + width = tileWidth, + height = tileHeight, + vertexBuffer = vertexBuffer, + 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() } /**