XCode调试技巧之EXC_BAD_ACCESS中BUG解决

本文介绍了如何使用Xcode的Zombie Objects和Malloc Stack调试EXC_BAD_ACCESS错误。通过编辑Scheme启用这些选项,重新运行程序,根据控制台输出的log定位问题。当发现内存泄漏或僵尸对象时,可以使用malloc_history命令查找对象分配历史。问题通常出现在-[MyMobileViewController viewDidLoad]、-[MyMobileViewController initTheView]和-[MyMobileViewController initTheFourthView]方法中,通过断点调试可以找到并修复问题。调试完成后记得关闭Zombie模式和Malloc Stack以避免内存风险。
摘要由CSDN通过智能技术生成

1、通过编辑Edit Scheme,选择Diagnostics,勾选Enable Zombie Objects & Malloc Stack(注意、调试完毕后必须取消勾选,因为Zombile模式不会释放内存,很危险)



2、重新run你的程序,reproduce crash流程,控制台会输出如下log:



3、由于最新的Xcode不支持GDB模式在控制台输入指令,所以我们这里打开Max OS上的终端程序,输入指令:

malloc_history 3774 0x7d121870

注意:这里的3774对应控制台log的MobileFlagship后面的第一组数字,这个是你crash的pid号,别搞错了

  后面的地址就不用说了,自己copy


4、输入完指令后,终端会输出相关的历史信息:

Last login: Sat May  2 10:26:34 on ttys003

wurongbiaos-Mac-mini:~ wurongbiao$ malloc_history 3774 0x7d121870

malloc_history Report Version:  2.0

Process:         MobileFlagship [3774]

Path:            /Users/wurongbiao/Library/Developer/CoreSimulator/Devices/1A5BC93D-EDC2-44E4-8D0D-21253866FDEC/data/Containers/Bundle/Application/E0532AFD-4997-4DBE-881C-D297E1942FE6/MobileFlagship.app/MobileFlagship

Load Address:    0x58000

Identifier:      MobileFlagship

Version:         ???

Code Type:       X86

Parent Process:  debugserver [3775]


Date/Time:       2015-05-02 11:26:43.628 +0800

OS Version:      Mac OS X 10.10.2 (14C1510)

Report Version:  7

Analysis Tool:   /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/bin/malloc_history

Analysis Tool Version:  iOS Simulator 8.1 (12B411)

----


Invalid connection: com.apple.coresymbolicationd

ALLOC 0x7d121870-0x7d1218ef [size=128]: thread_4fd11d4 |start | main | UIApplicationMain | GSEventRun | GSEventRunModal | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopRun | __CFRunLoopDoSources0 | __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ | _UIApplicationHandleEventQueue | +[CATransaction flush] | CA::Transaction::commit() | CA::Context::commit_transaction(CA::Transaction*) | CA::Layer::layout_and_display_if_needed(CA::Transaction*) | CA::Layer::layout_if_needed(CA::Transaction*) | -[CALayer layoutSublayers] | -[NSObject performSelector:withObject:] | -[UIView(CALayerDelegate) layoutSublayersOfLayer:] | -[UILayoutContainerView layoutSubviews] | -[UINavigationController __viewWillLayoutSubviews] | -[UINavigationController _startDeferredTransitionIfNeeded:] | -[UINavigationController _startCustomTransition:] | -[UIViewController view] | -[UIViewController loadViewIfRequired] | -[MyMobileViewController viewDidLoad] | -[MyMobileViewController initTheView] | -[MyMobileViewController initTheFourthView] | -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] | -[UINib instantiateWithOwner:options:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | UINibDecoderDecodeObjectForValue | -[UIRuntimeEventConnection initWithCoder:] | -[UIRuntimeConnection initWithCoder:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | -[UIClassSwapper initWithCoder:] | -[UIView initWithCoder:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | UINibDecoderDecodeObjectForValue | -[UIImageView initWithCoder:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | -[UIImageNibPlaceholder initWithCoder:] | _UIImageAtPath | _UICacheNameForImageAtPath | +[NSString stringWithFormat:] | -[NSPlaceholderString initWithFormat:locale:arguments:] | _CFStringCreateWithFormatAndArgumentsAux2 | __CFStringAppendFormatCore | CFStringAppend | __CFStringChangeSizeMultiple | CFAllocatorAllocate | __CFAllocatorSystemAllocate | malloc_zone_malloc 

----

FREE  0x7d121870-0x7d1218ef [size=128]: thread_4fd11d4 |start | main | UIApplicationMain | GSEventRun | GSEventRunModal | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopRun | __CFRunLoopDoSources0 | __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ | _UIApplicationHandleEventQueue | +[CATransaction flush] | CA::Transaction::commit() | CA::Context::commit_transaction(CA::Transaction*) | CA::Layer::layout_and_display_if_needed(CA::Transaction*) | CA::Layer::layout_if_needed(CA::Transaction*) | -[CALayer layoutSublayers] | -[NSObject performSelector:withObject:] | -[UIView(CALayerDelegate) layoutSublayersOfLayer:] | -[UILayoutContainerView layoutSubviews] | -[UINavigationController __viewWillLayoutSubviews] | -[UINavigationController _startDeferredTransitionIfNeeded:] | -[UINavigationController _startCustomTransition:] | -[UIViewController view] | -[UIViewController loadViewIfRequired] | -[MyMobileViewController viewDidLoad] | -[MyMobileViewController initTheView] | -[MyMobileViewController initTheFourthView] | -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] | -[UINib instantiateWithOwner:options:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | UINibDecoderDecodeObjectForValue | -[UIRuntimeEventConnection initWithCoder:] | -[UIRuntimeConnection initWithCoder:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | -[UIClassSwapper initWithCoder:] | -[UIView initWithCoder:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | UINibDecoderDecodeObjectForValue | -[UIImageView initWithCoder:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | -[UIImageNibPlaceholder initWithCoder:] | _UIImageAtPath | _UICacheNameForImageAtPath | +[NSString stringWithFormat:] | -[NSPlaceholderString initWithFormat:locale:arguments:] | _CFStringCreateWithFormatAndArgumentsAux2 | __CFStringAppendFormatCore | CFStringAppend | __CFStringChangeSizeMultiple | CFAllocatorDeallocate | __CFAllocatorSystemDeallocate | malloc_zone_free 


ALLOC 0x7d121870-0x7d12189f [size=48]: thread_4fd11d4 |start | main | UIApplicationMain | GSEventRun | GSEventRunModal | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopRun | __CFRunLoopDoSources0 | __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ | _UIApplicationHandleEventQueue | +[CATransaction flush] | CA::Transaction::commit() | CA::Context::commit_transaction(CA::Transaction*) | CA::Layer::layout_and_display_if_needed(CA::Transaction*) | CA::Layer::layout_if_needed(CA::Transaction*) | -[CALayer layoutSublayers] | -[NSObject performSelector:withObject:] | -[UIView(CALayerDelegate) layoutSublayersOfLayer:] | -[UILayoutContainerView layoutSubviews] | -[UINavigationController __viewWillLayoutSubviews] | -[UINavigationController _startDeferredTransitionIfNeeded:] | -[UINavigationController _startCustomTransition:] | -[UIViewController view] | -[UIViewController loadViewIfRequired] | -[MyMobileViewController viewDidLoad] | -[MyMobileViewController initTheView] | -[MyMobileViewController initTheFourthView] | -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] | -[UINib instantiateWithOwner:options:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | UINibDecoderDecodeObjectForValue | -[UIRuntimeEventConnection initWithCoder:] | -[UIRuntimeConnection initWithCoder:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | -[UIClassSwapper initWithCoder:] | -[UIView initWithCoder:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | UINibDecoderDecodeObjectForValue | -[UIImageView initWithCoder:] | -[UIView initWithCoder:] | UIViewCommonInitWithFrame | -[UIView _createLayerWithFrame:] | _objc_rootAlloc | class_createInstance | calloc | malloc_zone_calloc 


Binary Images:

   0x58000 -   0x6fafe3 +com.whty.MobileFlagship (1.3 - 1.3.2) <B43C39D8-1D2F-357C-AF00-482E165C9E3C> /Users/wurongbiao/Library/Developer/CoreSimulator/Devices/1A5BC93D-EDC2-44E4-8D0D-21253866FDEC/data/Containers/Bundle/Application/E0532AFD-4997-4DBE-881C-D297E1942FE6/MobileFlagship.app/MobileFlagship

  0xabd000 -   0xae123b +dyld_sim (0.0 - ???) <1DD50FCA-ADA6-37E4-8BCD-39F9E2BE13F3> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/lib/dyld_sim

  0xb19000 -   0xb20ff3 +libBacktraceRecording.dylib (56044) <C89F4E28-E01B-3EF9-8272-7A8E22255040> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/lib/libBacktraceRecording.dylib

  0xb27000 -   0xb36fff +libz.1.dylib (55) <CB7BA848-2C4F-3E6A-9168-161B2BAE45AA> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/lib/libz.1.dylib

  0xb3c000 -   0xba3fff  com.apple.SystemConfiguration (1.14 - 1.14) <D6E77CAA-A5DF-35A3-903C-3AD18D15A2D7> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration

  0xbd0000 -   0xd21ff3  com.apple.CoreText (352.0 - 450.2) <BA449391-A5CB-34E9-9DF2-51C8E16A1C57> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/CoreText.framework/CoreText

  0xdab000 -   0xf9bffb +libicucore.A.dylib (531.27.3) <1A4B6CEE-8AAC-3FB7-889B-F2CBF6071DB8> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/lib/libicucore.A.dylib

 0x1043000 -  0x10faffb  com.apple.coretelephony (113 - 2338) <9010E8D1-05ED-3C74-98F8-935D21AC2FCA> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/CoreTelephony.framework/CoreTelephony

 0x1156000 -  0x124affb +libiconv.2.dylib (42) <27D5E6E1-1255-35F9-9FE0-A6ED6ED4800C> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/lib/libiconv.2.dylib

 0x1259000 -  0x1270ff7  com.apple.CoreVideo (1.8 - 144.1) <448E89B4-F2F5-3310-9D91-6FCE329E6344> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/CoreVideo.framework/CoreVideo

 0x1283000 -  0x12fbff7  com.apple.CoreMedia (1.0 - 1560.67) <A4713680-D3E9-33AB-8997-4C2B92E72609> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/CoreMedia.framework/CoreMedia

 0x1346000 -  0x151fff7  com.apple.avfoundation (2.0 - 887.50) <B37D1B16-EE46-375F-AB5A-AC91625C5BA0> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/AVFoundation.framework/AVFoundation

 0x1667000 -  0x1744ff3  com.apple.MobileCoreServices (66 - 66) <C97C437D-052A-3247-8882-878C98671448> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/MobileCoreServices.framework/MobileCoreServices

 0x17a6000 -  0x1a30ffb  com.apple.CFNetwork (711.1.12 - 711.1.12) <5E16EF0B-03FE-30B7-91B2-7B490583E8A1> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/CFNetwork.framework/CFNetwork

 0x1b7b000 -  0x1d1cffb  com.apple.QuartzCore (1.10 - 361.1) <F1F234B5-0A78-398E-83D5-97764ED677F0> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/QuartzCore.framework/QuartzCore

 0x1dbd000 -  0x1ec7ffb +libxml2.2.dylib (25.6) <C5EF2A2A-3876-3B99-98B8-67C62FDD3561> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/lib/libxml2.2.dylib

 0x1ef7000 -  0x20afff7 +CoreGraphics (747.1) <A314AEF7-DCD0-34FA-8FDA-CEE4DC4290F2> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics

 0x210b000 -  0x2d10ffb  com.apple.UIKit (1.0 - 1000) <147232EB-68CB-3646-9BBA-D33A37E21304> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/UIKit.framework/UIKit

 0x32e8000 -  0x35ceff7  com.apple.Foundation (6.9 - 1141.1) <E1BBC567-EF3A-327C-960C-FD825DDF7049> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/Foundation.framework/Foundation

 0x3748000 -  0x393c45f +libobjc.A.dylib (647.1) <431C3B3E-89DC-33C8-8BB0-B7633153F899> /Ap

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值