cannot access an invalid/free’d bitmap here!

Android应用崩溃日志分析:Bitmap回收导致的问题及解决方案
文章讲述了在Android开发中遇到的一个错误日志,该日志指出由于在Dialog未关闭时回收了Bitmap导致应用崩溃。解决方案是在Dialog设置OnDismissListener,在Dialog消失时安全地回收Bitmap,以防止访问无效的数据库项。

报错日志

2023-07-19 19:05:43.780 11493-11493/? A/DEBUG: Build fingerprint: 'rockchip/rk3326_go/rk3326_go:9/PQ2A.190305.002/root08251804:userdebug/test-keys'
2023-07-19 19:05:43.780 11493-11493/? A/DEBUG: Revision: '0'
2023-07-19 19:05:43.781 11493-11493/? A/DEBUG: ABI: 'arm'
2023-07-19 19:05:43.781 11493-11493/? A/DEBUG: pid: 11078, tid: 11078, name: .org.video_lock  >>> com.org.video_lock <<<
2023-07-19 19:05:43.781 11493-11493/? A/DEBUG: signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
2023-07-19 19:05:43.781 11493-11493/? A/DEBUG: Abort message: 'Error, cannot access an invalid/free'd bitmap here!'
2023-07-19 19:05:43.781 11493-11493/? A/DEBUG:     r0  00000000  r1  00002b46  r2  00000006  r3  00000008
2023-07-19 19:05:43.781 11493-11493/? A/DEBUG:     r4  00002b46  r5  00002b46  r6  ffd42644  r7  0000010c
2023-07-19 19:05:43.781 11493-11493/? A/DEBUG:     r8  e6d8b480  r9  00000000  r10 00000000  r11 ffd42b9c
2023-07-19 19:05:43.782 11493-11493/? A/DEBUG:     ip  ffd425e0  sp  ffd42630  lr  e8367e49  pc  e835fc96

之前看到这种崩溃日志的时候,我都要崩溃了,还好里面有关键信息,可以帮助排查cannot access an invalid/free’d bitmap here!


我出现这个问题的原因是bitmap 显示在dialog 上面,而在这个Dialog还未消失时,就调用 bitmap.recycle() 回收bitmap 造成的

所以 所以,一定确保bitmap不再使用,再去回收它
在以下情况下应该回收 bitmap:

  1. 当你确定不再需要 bitmap 时,比如在 Activity 或 Fragment 的 onDestroy() 方法中。
  2. 如果 bitmap 已经从 UI 中移除,不再显示在任何 UI 元素上。
  3. 当需要释放内存或减少资源占用时。

解决办法就是在Dialog 设置 setOnDismissListener 监听,消失的时候调用

  // 回收 bitmap
        if (bitmap != null) {
            bitmap.recycle();
            bitmap = null;
        }
Cmdline: com.tencent.mm pid: 11091, tid: 15081, name: RenderThread >>> com.tencent.mm <<< uid: 10369 tagged_addr_ctrl: 0000000000000001 (PR_TAGGED_ADDR_ENABLE) pac_enabled_keys: 000000000000000f (PR_PAC_APIAKEY, PR_PAC_APIBKEY, PR_PAC_APDAKEY, PR_PAC_APDBKEY) signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr -------- Abort message: 'Error, cannot access an invalid/free'd bitmap here!' x0 0000000000000000 x1 0000000000003ae9 x2 0000000000000006 x3 00000073d76f3a40 x4 0000000000ffff5a x5 0000000000ffff5a x6 0000000000ffff5a x7 0000000000ff09b0 x8 00000000000000f0 x9 944d1504a1de4c41 x10 0000000000000001 x11 0000007555f6e420 x12 000000000136688c x13 0000000000000005 x14 000000083b2f624a x15 0000000000000000 x16 0000007555fdf828 x17 0000007555fc34c0 x18 000000720650c000 x19 0000000000002b53 x20 0000000000003ae9 x21 00000000ffffffff x22 00000074c0a46000 x23 00000073d76f4880 x24 00000074c0a48040 x25 00000073d76f4880 x26 00000073d76f41c0 x27 0000000000000438 x28 0000000000000040 x29 00000073d76f3ac0 lr 0000007555f57628 sp 00000073d76f3a40 pc 0000007555f5764c pst 0000000000001000 12 total frames backtrace: #00 pc 000000000008b64c /apex/com.android.runtime/lib64/bionic/libc.so (abort+156) (BuildId: 5ce1b77804344241efd50e639525e040) #01 pc 0000000000c549fc /apex/com.android.art/lib64/libart.so (art::Runtime::Abort(char const*)+812) (BuildId: e552e0387079dfdfcff9957d79905af9) #02 pc 0000000000016a10 /apex/com.android.art/lib64/libbase.so (android::base::SetAborter(std::__1::function<void (char const*)>&&)::$_0::__invoke(char const*)+80) (BuildId: d0380dc633cea55dfe4ba7fdda236219) #03 pc 000000000000d270 /system/lib64/liblog.so (__android_log_assert+288) (BuildId: bf5ca2aa66c2950e862801345c5d1195) #04 pc 000000000054a680 /system/lib64/libhwui.so (android::CopyRequestAdapter::getDestinationBitmap(int, int)+256) (BuildId: af10fe0c769646406dfe34024c2ebbaa) #05 pc 00000000005d3754 /system/lib64/libhwui.so (android::uirenderer::Readback::copySurfaceInto(ANativeWindow*, std::__1::shared_ptr<android::uirenderer::CopyRequest> const&)+1204) (BuildId: af10fe0c769646406dfe34024c2ebbaa) #06 pc 0000000000592604 /system/lib64/libhwui.so (std::__1::__function::__func<android::uirenderer::renderthread::RenderProxy::copySurfaceInto(ANativeWindow*, std::__1::shared_ptr<android::uirenderer::CopyRequest>&&)::$_0, std::__1::allocator<android::uirenderer::renderthread::RenderProxy::copySurfaceInto(ANativeWindow*, std::__1::shared_ptr<android::uirenderer::CopyRequest>&&)::$_0>, void ()>::operator()() (.__uniq.110435125376433984577876079601927332169.05736997f38264dc681cd19209baffd7)+36) (BuildId: af10fe0c769646406dfe34024c2ebbaa) #07 pc 0000000000344a94 /system/lib64/libhwui.so (android::uirenderer::renderthread::RenderThread::threadLoop()+500) (BuildId: af10fe0c769646406dfe34024c2ebbaa) #08 pc 0000000000019368 /system/lib64/libutils.so (android::Thread::_threadLoop(void*)+248) (BuildId: e41f2bc353059dc14cf4ee6b493447a4) #09 pc 000000000001bac4 /system/lib64/libutils.so (libutil_thread_trampoline(void*) (.__uniq.226528677032898775202282855395389835431)+20) (BuildId: e41f2bc353059dc14cf4ee6b493447a4) #10 pc 000000000009c8f8 /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start(void*)+232) (BuildId: 5ce1b77804344241efd50e639525e040) #11 pc 000000000008ee50 /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+64) (BuildId: 5ce1b77804344241efd50e639525e040)
最新发布
11-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值