在XCode的以前版本中,如果遇到了
[代碼]c#/cpp/oc代碼:
1 |
message sent to deallocated instance 0x6d564f0 |
在新的XCode里,調試器默認使用LLDB,我就講一下如何在LLDB狀態下定位到由於內存操作不當引起的Crash
首先我有一段會發生崩潰的代碼:
[代碼]c#/cpp/oc代碼:
1 |
NSString *themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:themePathTmp]; |
2 |
if (themePath) |
3 |
self.backgroundIV.image = [UIImage imageWithContentsOfFile:[themePath stringByAppendingPathComponent: @"mask_1.png" ]]; |
4 |
|
5 |
[themePath release]; |

運行代碼,出現下面的崩潰現象

下面我們打開“活動監視器”,找到我們對應的PID,我們的Target為HPThemeManager,只要找到HPThemeManager對應的PID就可以(HPThemeManager是在論壇里下載的,本來正在看代碼,就直接拿他來作試驗了)

現在,我們得到了兩個主要的信息:
進程ID:50127
崩潰地址:0x6d564f0
我們打開終端,輸入以下命令:
[代碼]c#/cpp/oc代碼:
1 |
sudo malloc_history 50127 0x6d564f0 |
結果顯示為:

這樣我們用能定位到這行代碼
[代碼]c#/cpp/oc代碼:
1 |
NSString *themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:themePathTmp]; |
對themePath進行排查,就找到了崩潰的罪魁禍首
[代碼]c#/cpp/oc代碼:
1 |
[themePath release]; |