日期:2014-05-16  浏览次数:21382 次

MallocStackLogging 的设置方法 查找 EXC_BAD_ACCESS 问题根源的方法

IOS 开发中,如果提前释放一个指针的内存,在以后还继续使用这个指针,那么程序会立刻 crash 掉,而且很难有报错信息,我以前都是靠猜测去判断错误的原因,我们应该利用工具去找到错误的地方,然后快速准确的定位到错误地方,及其错误原因,最后进行改进。

    其实 iOS 控制台提供这种机制,如果你选择 debug 模式(必须在这个模式下),在程序 crash 之后,在控制台输入 bt,就可以显示 crash 堆栈:

    Program received signal:  “EXC_BAD_ACCESS”.
    warning: Unable to read symbols for /Developer/ios4.2.1/Platforms/iPhoneOS.platform/DeviceSupport/4.2.1 (8C148)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
    (gdb) bt
    #0  0x33a06464 in objc_msgSend ()
    #1  0x3139de2e in -[UIImageView setImage:] ()
    #2  0x00009ecc in -[RoundMenuView touchesEnded:withEvent:] (self=0x29e140, _cmd=0x316b1a7b, touches=0x2e1050, event=0x2424f0) at /Users/wangjun/workspace/iphone/Classes/RoundMenuView.m:130
    #3  0x313b1354 in -[UIWindow _sendTouchesForEvent:] ()
    #4  0x313b0cce in -[UIWindow sendEvent:] ()
    #5  0x3139bfc6 in -[UIApplication sendEvent:] ()
    #6  0x3139b906 in _UIApplicationHandleEvent ()
    #7  0x31eecf02 in PurpleEventCallback ()
    #8  0x304236fe in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
    #9  0x304236c2 in __CFRunLoopDoSource1 ()
    #10 0x30415f7c in __CFRunLoopRun ()
    #11 0x30415c86 in CFRunLoopRunSpecific ()
    #12 0x30415b8e in CFRunLoopRunInMode ()
    #13 0x31eec4aa in GSEventRunModal ()<