message sent to deallocated instance 除錯

本文介绍了一种在iOS开发过程中定位并解决僵尸对象错误的方法,通过设置特定的运行参数和使用gdb命令来追踪错误来源。

原文地址:http://kenobiluh.blogspot.com/2011/04/message-sent-to-deallocated-instance.html


常常程式一長,哪邊就不小心多release了一次
這時候編譯器就只會告訴你:BAD_ACCESS,然後程式就死了
剛開始會google到去Argument加個NSZombieEnabled YES
會多吐一點東西讓你把bug除掉
今天遇到加了這個後error message變:
[CALayer release]:message sent to deallocated instance 0x4dd650
layer這麼多怎麼知道哪裡出問題阿???
google了一下才找到解法:


1.在Argument裡面加入這三個參數:
NSZombieEnabled YES
MallocStackLogging YES
MallocStackLoggingNoCompact Yes
第一項可監控deallocated的記憶體,給更多的錯誤訊息
第二項可開啟MallocStack,就知道記憶體在程式運行中被配置的歷史
第三項可以更清楚顯示指定的MallocStack狀況(一開始沒加看到快脫窗還是看不懂)
2.跑程式(建議用模擬器),開console,這時候可以注意到一開始會出現類似下列訊息:
myproject(11779) malloc: stack logs being written into /tmp/stack-logs.11779.myproject.81hXWV
表示gdb開始有在紀錄
3.讓程式跑到出錯
如果有做步驟一,應該就會看到message sent to deallocated instance的錯誤訊息
複製後面跟的位址
4.在(gdb)後面下指令info malloc-history 0x4dd650(剛剛得到的位址)
如果gdb說找不到指令,可改用shell 11779 malloc_history(11779為程式的pid)
建議在模擬器跑的原因是因為程式跑在裝置的OS上,pid是裝置給予的,要存取好像會有點問題
今天在這卡關卡了一陣
5.如果上述步驟順利的話就會看到一串比較像程式碼的東西,應該也就看得出bug了

像這次遇到的bug就是因為某個UIButton沒有給定記憶體位置,dealloc函式裡又dealloc了一次
最後當在main
這種bug單看error message最好是de得出來啦!

補記一下加入Argument的方法:
xcode 3系列:Executables→Get Info→Argument標籤
xcode 4系列:選取模擬器或裝置編譯的地方→edit scheme→Argument

Implementation Erase The erase functionality is implemented using the UNMAP command and it is enabled if the bProvisioningType parameter in the Unit Descriptor is set to 03h (TPRZ = 1). The device behavior shall comply with the UNMAP definition in [SBC] when the TPRZ bit in the READ CAPACITY(16) parameter data is set to one. As defined in [SBC], • The UNMAP command causes a mapped LBA to transition from mapped state to deallocated state if an unmap operation completes without error. • Since the TPRZ bit is set to one if the erase functionality is enabled, a READ command specifying a deallocated LBA shall return zero. • The device server may maintain a deallocated LBA in deallocated state until a write operation specifying that LBA is completed without error. • Or, the device server may transition a deallocated LBA from deallocated state to mapped state at any time (autonomous state transition). For UFS, if TPRZ bit is set to one and an autonomous transition to the mapped state occurs, the LBA shall be mapped to a physical block(s) containing data with all bits set to zero. LBA’s to be erased may be aligned to multiples of the dEraseBlockSize parameter value, where it is possible, to minimize performance impact. dEraseBlockSize is a parameter included in the Unit Descriptor. Discard The discard functionality is implemented using the UNMAP command and it is enabled if the bProvisioningType parameter in the Unit Descriptor is set to 02h (TPRZ = 0). The device behavior shall comply with the UNMAP definition in [SBC] when the TPRZ bit in the READ CAPACITY(16) parameter data is set to zero. As defined in [SBC], • The UNMAP command causes a mapped LBA to transition from mapped state to deallocated state if an unmap operation completes without error. • Since the TPRZ bit is set to zero if the discard functionality is enabled, a READ command specifying a deallocated LBA may return any data. • The device server may maintain a deallocated LBA in deallocated state until a write operation specifying that LBA is completed without error. • Or, the device server may transition a deallocated LBA from deallocated state to mapped state at any time (autonomous state transition). For UFS, if TPRZ bit is set to zero and an autonomous transition to the mapped state occurs, the LBA shall be mapped to a physical block(s) containing any data including the original data before UNMAP operation. LBA’s to be discarded may align to multiples of the dEraseBlockSize where possible to minimize performance impact. dEraseBlockSize is a parameter included in the Unit Descriptor. 翻译中文
最新发布
10-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值