如何排查EXC_BAD_ACCESS错误(Debugger使用GDB时适用)

刚开始学obj-c,遇到这个问题,网上搜了下,说的都有点短(本人愚钝,理解能力弱~~)。搞定后发上来,记录、共享。。

我的xcode是4.2的

操作如下:

1、增加NSZombieEnabled和MallocStackLogging环境变量:菜单Product--》Edit Scheme...,在弹出的窗口上方选择自己的项目及模拟器,左侧选择"Run ***.app"(***是你的项目名称),右侧选择Arguments选项卡,在Environment Variables中增加Name: NSZombieEnabled--》Value:YES、Name: MallocStackLogging--》Value:YES,并点击2个环境变量左侧的复选框,保证在启用状态。

2、debug自己的项目,出现EXC_BAD_ACCESS错误的时候,在控制台会出现类似下面的log

 

 写道
fbft(1431) malloc: recording malloc stacks to disk using standard recorder
fbft(1431) malloc: process 1249 no longer exists, stack logs deleted from /tmp/stack-logs.1249.fbft.bBsBO7.index
fbft(1431) malloc: stack logs being written into /tmp/stack-logs.1431.fbft.Fz32vw.index
2012-01-18 11:38:55.118 fbft[ 1431:ef03] *** -[_ UIResizableImage release]: message sent to deallocated instance 0x7245470

 从最后一行可以看出是调用了已经被dealloc的对象。(UIResizableImage应该是已经release的对象类型)

3、在控制台的(gdb)后面输入shell malloc_history 1431 0x7245470(其中1431和0x7245470是上面log中的pid和address),并回车。得到如下信息

 

 写道
......

ALLOC 0x7245470-0x72454af [size=64]: thread_b024f000 |thread_start | _pthread_start | _ZN2KBL14BackgroundLoadEPv | KB::DynamicDictionaryImpl::background_load_address_book(KB::StaticDictionary const&) | KB::fill_with_matchable_strings_from_address_book(KB::Hashmap<KB::String, bool>&) | ABAddressBookCreate | ABCCreateAddressBookWithDatabaseDirectory | ABCCreateAddressBookWithDatabaseDirectoryAndForceInProcessMigrationInProcessLinkingAndResetSortKeys | ABCDBContextCreateWithPathAndAddressBook | CPSqliteDatabaseRegisterFunction | CPSqliteDatabaseConnectionForWriting | _connectAndCheckVersion | _createConnectionForWriting | CPSqliteConnectionPerformSQL | CPSqliteConnectionStatementForSQLAndIgnoreErrors | CPSqlitePreparedStatement | sqlite3_prepare_v2 | sqlite3LockAndPrepare | sqlite3Prepare | sqlite3RunParser | sqlite3Parser | yy_reduce | sqlite3Pragma | sqlite3Init | sqlite3InitOne | sqlite3_exec | sqlite3InitCallback | sqlite3LockAndPrepare | sqlite3Prepare | sqlite3RunParser | sqlite3Parser | yy_reduce | sqlite3ExprAlloc | sqlite3DbMallocRaw | sqlite3MemMalloc | malloc_zone_malloc
----
FREE 0x7245470-0x72454af [size=64]: thread_b024f000 |thread_start | _pthread_start | _ZN2KBL14BackgroundLoadEPv | KB::DynamicDictionaryImpl::background_load_address_book(KB::StaticDictionary const&) | KB::fill_with_matchable_strings_from_address_book(KB::Hashmap<KB::String, bool>&) | ABAddressBookCreate | ABCCreateAddressBookWithDatabaseDirectory | ABCCreateAddressBookWithDatabaseDirectoryAndForceInProcessMigrationInProcessLinkingAndResetSortKeys | ABCDBContextCreateWithPathAndAddressBook | CPSqliteDatabaseRegisterFunction | CPSqliteDatabaseConnectionForWriting | _connectAndCheckVersion | _createConnectionForWriting | CPSqliteConnectionPerformSQL | CPSqliteConnectionStatementForSQLAndIgnoreErrors | CPSqlitePreparedStatement | sqlite3_prepare_v2 | sqlite3LockAndPrepare | sqlite3Prepare | sqlite3RunParser | sqlite3Parser | yy_reduce | sqlite3Pragma | sqlite3Init | sqlite3InitOne | sqlite3_exec | sqlite3InitCallback | sqlite3LockAndPrepare | sqlite3Prepare | sqlite3RunParser | sqlite3Parser | yy_reduce | sqlite3ExprDelete | sqlite3ExprDelete | sqlite3ExprDelete | sqlite3ExprDelete | sqlite3ExprDelete | sqlite3ExprListDelete | sqlite3ExprDelete | sqlite3DbFree | sqlite3MemFree | malloc_zone_free

ALLOC 0x7245470-0x7245493 [size=36]: thread_ad12a2c0 |start | main | UIApplicationMain | -[UIApplication _run] | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopRun | __CFRunLoopDoSource1 | __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ | PurpleEventCallback | _UIApplicationHandleEvent | -[UIApplication sendEvent:] | -[UIApplication handleEvent:withNewEvent:] | -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] | -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] | -[FbftAppDelegate application:didFinishLaunchingWithOptions:] | -[UIWindow makeKeyAndVisible] | -[UIWindow _orderFrontWithoutMakingKey] | -[UIWindow _setHidden:forced:] | -[UIWindow addRootViewControllerViewIfPossible] | -[UIViewController view] | -[ TestViewController viewDidLoad] | -[UIImage(UIImageDeprecated) stretchableImageWithLeftCapWidth:topCapHeight :] | -[UIImage(UIImagePrivate) _stretchableImageWithCapInsets:] | -[UIImage resizableImageWithCapInsets:] | _objc_rootAlloc | +[NSObject allocWithZone:] | _objc_rootAllocWithZone | class_createInstance | calloc | malloc_zone_calloc

Binary Images:
0x1000 - 0x3ffb +fbft (??? - ???) <92BCD2F3-83ED-36DD-A56A-37022E0229E0> /Users/fangjiawang/Library/Application Support/iPhone Simulator/5.0/Applications/8ED06429-A51E-4D6C-9ACC-C9A6E269C80A/fbft.app/fbft
0x7000 - 0x612fff +UIKit (1600.0.0 - comp
.......

 从上面标红的log中可以大概知道是哪里的代码出了问题,TestViewController类中的stretchableImageWithLeftCapWidth:topCapHeight方法中引入了已经release对象

 

PS-疑问:配置的两个环境变量,对发布出来的应用有影响吗?有答案的留下言~ 谢谢~~

你可能感兴趣的:(exc_bad_access)