Xcode 调试技巧(持续更新)
iOS App开发过程中不可避免地遇到程序崩溃的问题。当程序崩溃时,我们首先要找到它崩溃的原因。一旦找到原因,问题就容易解决了。Xcode Debugger是查找崩溃原因的有利工具,我们应该学会熟练使用它,迅速解决问题,节约宝贵的开发时间。
崩溃在main( )
添加Exception Breakpoint
Project > Breakpoint navigator > +(Bottom left)> Add Exception Breakpoint
符号断点
符号断点是我们验证某个方法是否被调用的一种方法。添加方法:
Project > Breakpoint navigator > +(Bottom left)> Add Symbolic Breakpoint
例如:application:DidFinishLaunchingWithOptions:
。
打印方法名
1
|
|
控制台打印
1 2 3 4 5 6 |
|
打印异常信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
输出View结构
任意时刻暂停App,在lldb中输入:
1
|
|
SIGABRT
SIGABRT:SIGNAL ABORT(中止信号)。通常可以让程序继续运行,之后会输出些有助于定位问题的信息。
EXC_BAD_ACCESS
它出现的原因是因为访问一个已经释放的对象或向它发送消息。通常可以开启Zombie Objects(Toolbar > Edit Scheme… > Run > Diagnostics > Enabled Zombie Objects)重新运行程序以定位问题。
Note that you shouldn’t leave Zombie Objects enabled all the time. Because this tool never deallocates memory, but simply marks it as being undead, you end up leaking all over the place and will run out of free memory at some point. So only enable Zombie Objects to diagnose a memory-related error, and then disable it again.
Enabled Zombie Objects后,控制台通常会打印出*** -[CFNumber respondsToSelector:]: message sent to deallocated instance 0x31ab5cfe0
类似的信息,那么问题来了,我们怎么知道0x31ab5cfe0是哪个对象?
Apple Memory Usage Performace Guidelines中介绍了记录内存分配历史的方法,简述如下:
- 设置环境变量: MallocStackLogging,MallocStackLoggingNoCompact为1;
- 使用malloc_history命令找到相应的对象。
1 2 3 4 5 6 |
|
unrecognized selector sent to instance 0x7fa71400fc10
A:
- Add a exception breakpoint;
- Check the description of the object in memory address
po (NSObject*)(0x7fa71400fc10)
.
How to debug “unrecognized selector sent to instance”
error: property ‘frame’ not found on object of type ‘UIView *’
A:
1 2 |
|
Reference:error: property ‘frame’ not found on object of type ‘UIView *’
An @import-ant Change in Xcode
Reference:
- Memory Usage Performace Guidelines
- iOS 6 Programming Pushing the Limits
- My App Crashed, Now What? – Part 1
- My App Crashed, Now What? – Part 2
- Intermediate Debugging with Xcode 4.5
- Xcode调试技巧