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

如何排查EXC_BAD_ACCESS错误

刚开始学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 | __C