您的位置:首页 > 其它

Xcode调试之exc_bad_access以及 message sent to deallocated instance

2014-08-14 13:54 337 查看
如果出现exc_bad_access错误,基本上是由于内存泄漏,错误释放,对一个已经释放的对象进行release操作。但是xcode有时候不会告诉你错误在什么地方(Visual Studio这点做得很好)。不过我们可以通过设置xcode环境变量环境变量NSZombieEnabled,MallocStackLogging查看错误的来源:

1、菜单 Product > Manage Schemes;

2、选中当前的Scheme,点Edit按钮;

3、设置环境变量。在Arguments > Environment Variables;

4、添加环境变量MallocStackLogging,NSAutoreleaseFreedObjectCheckEnabled,MallocStackLoggingNoCompact,NSZombieEnabled并设置YES

5、最后记得把环境变量删除或设置为NO,因为它们会使得内存不会被释放.

message sent to deallocated instance后会有一个内存地址,如:0×6497860,我们需要查看该地址的malloc history.查看方法,在原来的gdb下,使用”info malloc_history 0×6497860“即可显示malloc记录。但是新版的Xcode 不再支持,怎么办呢?秀逗麻袋,我们还有terminal,使用终端的malloc_history命令,如”malloc_history 32009 0×6497860“或者“sudo malloc_history 32009 0×6497860”即可显示。其中的32009是该进程的pid,根据这个malloc记录,可以大致定位出错信息的代码位置。

会出现类似以下提示代码,根据提示就可以找出错误具体位置

ALLOC 0xfcdff50-0xfce00b7 [size=360]: thread_3bf2a28 |start | main | UIApplicationMain | GSEventRun
| GSEventRunModal | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopRun | __CFRunLoopDoObservers
| __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ | _afterCACommitHandler | _applyBlockToCFArrayCopiedToStack
| ___afterCACommitHandler_block_invoke | __38-[UITableView touchesEnded:withEvent:]_block_invoke |
 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] | -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] | -[MessageListViewController tableView:didSelectRowAtIndexPath:] | _objc_rootAlloc | class_createInstance | calloc | malloc_zone_calloc
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: