首先,XCode使用GDB进行调试。
当我们设置了断点的时候,在XCode中没有直接能够看到运行堆栈的方法。
为了看到,需要分以下几步:
1.Menu->Product->EditSchema->Run->Arguments
2.在Arguments页面中设置4个环境变量:
NSZombieEnabled=YES
MallocStackLogging=YES
MallocStackLoggingNoCompact=YES
NSDebugEnabled=YES
点击OK完成编辑
3.启动程序(直到运行到断点或者程序遇到崩溃消息)
在日志窗口能够看到如下日志:
2012-04-15 23:19:41.051 otest[3021:7803] ***-[CFDictionary release]: message sent to deallocated instance0x1cdde90
(gdb)
从这个日志可以看到两个内容,一个是程序的pid是3021,另一个是出现问题的对象实例的内存地址为0x1cdde90。接下来就可以利用这两个内容查看堆栈信息了。
4.打开Shell
运行命令:
malloc_history30210x1cdde90
执行结果如下:
xxxxxxxx:~ xxxxxxxx$ malloc_history 2793 0x93425a0 malloc_history Report Version: 2.0 Process: otest [2793] Path:/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/Developer/usr/bin/otest Load Address: 0x1000 Identifier: otest Version: ??? (???) Code Type: X86 (Native) Parent Process: gdb-i386-apple-darwin [2785] Date/Time: 2012-04-15 23:06:08.424 +0800 OS Version: Mac OS X 10.7 (11A511) Report Version: 7 ALLOC 0x93424c0-0x9342607 [size=328]: thread_ad2b02c0 |0x1f31 |0x1f8d | 0x2203 | 0x25be | 0x231c | +[NSObjectperformSelector:withObject:] | +[SenTestProbe runTests:] |+[SenTestProbe specifiedTestSuite] | +[SenTestSuitetestSuiteForBundlePath:] | +[SenTestSuite suiteForBundleCache] |+[SenTestSuite updateCache] | +[SenTestCase(Suite)defaultTestSuite] | +[SenTestSuite testSuiteForTestCaseClass:] |+[SenTestCase testInvocations] |+[NSObject(SenTestRuntimeUtilities) senAllInstanceInvocations] |+[NSObject(SenTestRuntimeUtilities) senAllInstanceInvocations] |+[NSObject(SenTestRuntimeUtilities) senAllInstanceInvocations] |+[NSObject(SenTestRuntimeUtilities) senAllInstanceInvocations] |+[NSObject(SenTestRuntimeUtilities) senInstanceInvocations] |-[SenTestInvocationEnumerator allObjects] |-[SenTestInvocationEnumerator nextObject] | +[NSInvocationinvocationWithMethodSignature:] | calloc | malloc_zone_calloc ---- FREE 0x93424c0-0x9342607 [size=328]: thread_ad2b02c0 |0x1f31 |0x1f8d | 0x2203 | 0x25be | 0x231c | +[NSObjectperformSelector:withObject:] | +[SenTestProbe runTests:] |+[SenTestProbe specifiedTestSuite] | -[NSAutoreleasePool drain] |_CFAutoreleasePoolPop | (anonymousnamespace)::AutoreleasePoolPage::pop(void*) | objc_release |_objc_rootRelease | -[__NSArrayM dealloc] | CFRelease |_objc_rootRelease | -[NSInvocation dealloc] | free ALLOC0x9342550-0x93425db [size=140]: thread_ad2b02c0 |0x1f31 | 0x1f8d |0x2203 | 0x25be | 0x231c | +[NSObject performSelector:withObject:]| +[SenTestProbe runTests:] | -[SenTest run] | -[SenTestSuiteperformTest:] | -[SenTest run] | -[SenTestSuite performTest:] |-[SenTest run] | -[SenTestCase performTest:] | -[SenTestCaseinvokeTest] | -[NSInvocation invoke] | __invoking___ |-[BankSvcTests testBankService] | NSLog | NSLogv | _CFLogvEx |__CFLogCString | CFCalendarCreateWithIdentifier |__CFCalendarCreateUCalendar | ucal_open |icu::Calendar::createInstance(icu::TimeZone*, icu::Localeconst&, UErrorCode&) |icu::GregorianCalendar::GregorianCalendar(icu::Localeconst&, UErrorCode&) |icu::Calendar::Calendar(icu::TimeZone*, icu::Localeconst&, UErrorCode&) |icu::Calendar::setWeekData(icu::Locale const&, charconst*, UErrorCode&) | ures_openDirect | malloc |malloc_zone_malloc ---- FREE 0x9342550-0x93425db [size=140]: thread_ad2b02c0 |0x1f31 |0x1f8d | 0x2203 | 0x25be | 0x231c | +[NSObjectperformSelector:withObject:] | +[SenTestProbe runTests:] |-[SenTest run] | -[SenTestSuite performTest:] | -[SenTest run] |-[SenTestSuite performTest:] | -[SenTest run] | -[SenTestCaseperformTest:] | -[SenTestCase invokeTest] | -[NSInvocation invoke]| __invoking___ | -[BankSvcTests testBankService] | NSLog | NSLogv| _CFLogvEx | __CFLogCString | CFCalendarCreateWithIdentifier |__CFCalendarCreateUCalendar | ucal_open |icu::Calendar::createInstance(icu::TimeZone*, icu::Localeconst&, UErrorCode&) |icu::GregorianCalendar::GregorianCalendar(icu::Localeconst&, UErrorCode&) |icu::Calendar::Calendar(icu::TimeZone*, icu::Localeconst&, UErrorCode&) |icu::Calendar::setWeekData(icu::Locale const&, charconst*, UErrorCode&) | 0x825435 | free ALLOC0x9342550-0x93425db [size=140]: thread_ad2b02c0 |0x1f31 | 0x1f8d |0x2203 | 0x25be | 0x231c | +[NSObject performSelector:withObject:]| +[SenTestProbe runTests:] | -[SenTest run] | -[SenTestSuiteperformTest:] | -[SenTest run] | -[SenTestSuite performTest:] |-[SenTest run] | -[SenTestCase performTest:] | -[SenTestCaseinvokeTest] | -[NSInvocation invoke] | __invoking___ |-[BankSvcTests testBankService] | NSLog | NSLogv | _CFLogvEx |__CFLogCString | CFCalendarCreateWithIdentifier |ucal_setGregorianChange |icu::GregorianCalendar::setGregorianChange(double,UErrorCode&) |icu::GregorianCalendar::GregorianCalendar(icu::TimeZoneconst&, UErrorCode&) |icu::Calendar::Calendar(icu::TimeZone const&,icu::Locale const&, UErrorCode&) |icu::Calendar::setWeekData(icu::Locale const&, charconst*, UErrorCode&) | ures_openDirect | malloc |malloc_zone_malloc ---- FREE 0x9342550-0x93425db [size=140]: thread_ad2b02c0 |0x1f31 |0x1f8d | 0x2203 | 0x25be | 0x231c | +[NSObjectperformSelector:withObject:] | +[SenTestProbe runTests:] |-[SenTest run] | -[SenTestSuite performTest:] | -[SenTest run] |-[SenTestSuite performTest:] | -[SenTest run] | -[SenTestCaseperformTest:] | -[SenTestCase invokeTest] | -[NSInvocation invoke]| __invoking___ | -[BankSvcTests testBankService] | NSLog | NSLogv| _CFLogvEx | __CFLogCString | CFCalendarCreateWithIdentifier |ucal_setGregorianChange |icu::GregorianCalendar::setGregorianChange(double,UErrorCode&) |icu::GregorianCalendar::GregorianCalendar(icu::TimeZoneconst&, UErrorCode&) |icu::Calendar::Calendar(icu::TimeZone const&,icu::Locale const&, UErrorCode&) |icu::Calendar::setWeekData(icu::Locale const&, charconst*, UErrorCode&) | 0x825435 | free ALLOC0x9342550-0x93425db [size=140]: thread_ad2b02c0 |0x1f31 | 0x1f8d |0x2203 | 0x25be | 0x231c | +[NSObject performSelector:withObject:]| +[SenTestProbe runTests:] | -[SenTest run] | -[SenTestSuiteperformTest:] | -[SenTest run] | -[SenTestSuite performTest:] |-[SenTest run] | -[SenTestCase performTest:] | -[SenTestCaseinvokeTest] | -[NSInvocation invoke] | __invoking___ |-[BankSvcTests testBankService] | NSLog | NSLogv | _CFLogvEx |__CFLogCString | CFCalendarCreateWithIdentifier |__CFCalendarCreateUCalendar | ucal_open |icu::Calendar::createInstance(icu::TimeZone*, icu::Localeconst&, UErrorCode&) |icu::GregorianCalendar::GregorianCalendar(icu::Localeconst&, UErrorCode&) |icu::Calendar::Calendar(icu::TimeZone*, icu::Localeconst&, UErrorCode&) |icu::Calendar::setWeekData(icu::Locale const&, charconst*, UErrorCode&) | 0x88429c | 0x884244 |ures_getByKeyWithFallback | 0x826cc2 | malloc |malloc_zone_malloc ---- FREE 0x9342550-0x93425db [size=140]: thread_ad2b02c0 |0x1f31 |0x1f8d | 0x2203 | 0x25be | 0x231c | +[NSObjectperformSelector:withObject:] | +[SenTestProbe runTests:] |-[SenTest run] | -[SenTestSuite performTest:] | -[SenTest run] |-[SenTestSuite performTest:] | -[SenTest run] | -[SenTestCaseperformTest:] | -[SenTestCase invokeTest] | -[NSInvocation invoke]| __invoking___ | -[BankSvcTests testBankService] | NSLog | NSLogv| _CFLogvEx | __CFLogCString | CFCalendarCreateWithIdentifier |__CFCalendarCreateUCalendar | ucal_open |icu::Calendar::createInstance(icu::TimeZone*, icu::Localeconst&, UErrorCode&) |icu::GregorianCalendar::GregorianCalendar(icu::Localeconst&, UErrorCode&) |icu::Calendar::Calendar(icu::TimeZone*, icu::Localeconst&, UErrorCode&) |icu::Calendar::setWeekData(icu::Locale const&, charconst*, UErrorCode&) | 0x884141 | 0x825435 | freeALLOC 0x9342550-0x93425cf [size=128]: thread_ad2b02c0 |0x1f31 |0x1f8d | 0x2203 | 0x25be | 0x231c | +[NSObjectperformSelector:withObject:] | +[SenTestProbe runTests:] |-[SenTest run] | -[SenTestSuite performTest:] | -[SenTest run] |-[SenTestSuite performTest:] | -[SenTest run] | -[SenTestCaseperformTest:] | -[SenTestCaseRun start] | -[SenTestRunpostNotificationName:] | -[SenTestRunpostNotificationName:userInfo:] | -[NSNotificationCenterpostNotificationName:object:userInfo:] | _CFXNotificationPost |___CFXNotificationPost_block_invoke_0 | __57-[NSNotificationCenteraddObserver:selector:name:object:]_block_invoke_0 | +[SenTestLogtestCaseDidStart:] | +[SenTestLog testLogWithFormat:] |+[SenTestLog testLogWithFormat:arguments:] | -[NSStringinitWithFormat:arguments:] | -[NSPlaceholderStringinitWithFormat:locale:arguments:] |_CFStringCreateWithFormatAndArgumentsAux |__CFStringAppendFormatCore | _NSDescriptionWithLocaleFunc |-[SenTestCase description] | -[SenTestCase name] | +[NSStringstringWithFormat:] | -[NSPlaceholderStringinitWithFormat:locale:arguments:] |_CFStringCreateWithFormatAndArgumentsAux |__CFStringAppendFormatCore | __CFStringAppendBytes |__CFStringChangeSizeMultiple | CFAllocatorAllocate |__CFAllocatorSystemAllocate | malloc_zone_malloc ---- FREE 0x9342550-0x93425cf [size=128]: thread_ad2b02c0 |0x1f31 |0x1f8d | 0x2203 | 0x25be | 0x231c | +[NSObjectperformSelector:withObject:] | +[SenTestProbe runTests:] |-[SenTest run] | -[SenTestSuite performTest:] | -[SenTest run] |-[SenTestSuite performTest:] | -[SenTest run] | -[SenTestCaseperformTest:] | -[SenTestCaseRun start] | -[SenTestRunpostNotificationName:] | -[SenTestRunpostNotificationName:userInfo:] | -[NSNotificationCenterpostNotificationName:object:userInfo:] | _CFXNotificationPost |___CFXNotificationPost_block_invoke_0 | __57-[NSNotificationCenteraddObserver:selector:name:object:]_block_invoke_0 | +[SenTestLogtestCaseDidStart:] | +[SenTestLog testLogWithFormat:] |+[SenTestLog testLogWithFormat:arguments:] | -[NSStringinitWithFormat:arguments:] | -[NSPlaceholderStringinitWithFormat:locale:arguments:] |_CFStringCreateWithFormatAndArgumentsAux |__CFStringAppendFormatCore | _NSDescriptionWithLocaleFunc |-[SenTestCase description] | -[SenTestCase name] | +[NSStringstringWithFormat:] | -[NSPlaceholderStringinitWithFormat:locale:arguments:] |_CFStringCreateWithFormatAndArgumentsAux | CFRelease |__CFStringDeallocate | CFAllocatorDeallocate |__CFAllocatorSystemDeallocate | malloc_zone_free ALLOC0x93425a0-0x93425cf [size=48]: thread_ad2b02c0 |0x1f31 | 0x1f8d |0x2203 | 0x25be | 0x231c | +[NSObject performSelector:withObject:]| +[SenTestProbe runTests:] | -[SenTest run] | -[SenTestSuiteperformTest:] | -[SenTest run] | -[SenTestSuite performTest:] |-[SenTest run] | -[SenTestCase performTest:] | -[SenTestCaseinvokeTest] | -[NSInvocation invoke] | __invoking___ |-[CurrencySvcTests testCurrencyService] | +[CurrencyServicesharedInstance] | +[CurrencyService sharedInstanceSynch] |-[CurrencyService init] | +[NSMutableDictionarydictionaryWithCapacity:] | -[__NSPlaceholderDictionaryinitWithCapacity:] | CFDictionaryCreateMutable |__CFDictionaryCreateGeneric | CFBasicHashCreate |_CFRuntimeCreateInstance | CFAllocatorAllocate |__CFAllocatorSystemAllocate | malloc_zone_malloc |