Skip to content

Commit b14fb94

Browse files
committed
Workaround memleak
1 parent 305dfd7 commit b14fb94

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

app/src/main/java/org/akanework/gramophone/logic/GramophoneAlbumArtProvider.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,30 +283,35 @@ class GramophoneAlbumArtProvider : ContentProvider() {
283283
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
284284
val ht = HandlerThread("pfd_${System.currentTimeMillis()}")
285285
ht.start()
286+
var bytesRef: ByteArray? = bytes
286287
val pfd = try {
287288
// Specifically ImageDecoder on Android P or later needs a seekable file descriptor
288289
context.getSystemService<StorageManager>()!!.openProxyFileDescriptor(
289290
ParcelFileDescriptor.MODE_READ_ONLY,
290291
object : ProxyFileDescriptorCallback() {
291292
override fun onGetSize(): Long {
292-
return bytes.size.toLong()
293+
return bytesRef!!.size.toLong()
293294
}
294295

295296
override fun onRead(offset: Long, size: Int, data: ByteArray): Int {
296297
val offset = offset.toInt()
297298
var size = size
298-
if (offset + size > bytes.size) {
299-
size = bytes.size - offset
299+
if (offset + size > bytesRef!!.size) {
300+
size = bytesRef!!.size - offset
300301
}
301302
System.arraycopy(
302-
bytes, offset, data, 0,
303+
bytesRef!!, offset, data, 0,
303304
size
304305
)
305306
return size
306307
}
307308

308309
override fun onRelease() {
309310
ht.quitSafely()
311+
// Before Android 15 QPR1, onRelease() is called but callback itself is
312+
// never garbage collected. So ensure we don't leak the bitmap in this
313+
// case too, to avoid large memory leaks due to this platform bug.
314+
bytesRef = null
310315
}
311316
}, Handler(ht.looper)
312317
)

0 commit comments

Comments
 (0)