exc_bad_access(code=1, address=0x789870)野指针错误原因查找

exc_bad_access(code=1, address=0x789870)野指针错误,主要的原因是,当某个对象被完全释放,也就是retainCount,引用计数为0后。再去通过该对象去调用其它的方法就会出现野指针错误。

例如:

Person *jerry = [[Person alloc]init]; // retainCount引用计数为1

[jerry eat]; // 调用吃的方法

[jerry release]; // 释放jerry这个对象到 retainCount引用计数为0

// 此时如果继续引用jerry这个对象就会出现野指针错误,exc_bad_access

[jerry sleep];

 

解决方法:

首先得定位到哪里出现的这样野指针引用错误,如果是大型的项目代码量一大,找起来是非常痛苦的。

IOS提供了一个环境变量设置来帮忙定位错误位置的信息描述:NSZombieEnabled ,就是当设置NSZombieEnabled环境变量后,一个对象销毁时会被转化为_NSZombie,设置NSZombieEnabled后,当你向一个已经释放的对象发送消息,这个对象就不会向之前那样Crash或者产生一个难以理解的行为,而是放出一个错误消息,然后以一种可预测的可以产生debug断点的方式消失, 因此我们就可以找到具体或者大概是哪个对象被错误的释放了。 

例如会出现这样的提示:

[jerry sleep]:message sent to deallocated instance 0x78d7ed0

设置NSZombieEnabled环境变量,XCode4中:

你可以点击 Xcode4 菜单 Product -> Edit Scheme-> Arguments, 然后将点击”加号”, 将 NSZombieEnabled 参数加到Environment Variables 窗口中, 后面的数值写上 ”YES”.

或者在 Xcode4 菜单 Product -> EditScheme -> Diagnostics 设置窗口中直接勾上Enable ZombieObjects 即可,Xcode 可用 cmd+shift+< 进到这个窗口。

NSZombieEnabled

 1. Xcode内置GDB,可以使用GDB调试,调试命令:

1.1 po 命令:为 print object 的缩写,显示对象的文本描述

(lldb) po [$eax class]:输出异常对象的地址


(lldb) po [$eax name]:输出这个异常的名字


(lldb) po [$eax reason]:这个将会输出错误消息:


(lldb)  “po $eax”:对这个对象调用“description”方法和打印出来

“$eax”是cup的一个寄存器。在一个异常的情况下,这个寄存器将会包含一个异常对象的指针。注意:$eax只会在模拟器里面工作,假如你在设备上调试,你将需要使用”$r0″寄存器

1.2 print 命令:有点类似于格式化输出,可以输出对象的不同信息

比如:print (char*)[[dic description] cString]、(lldb) print (int)[label retainCount]

1.3 info 命令:我们可以查看内存地址所在信息

1.4 info line *内存地址:可以获取内存地址所在的代码行相关信息

1.5 show 命令:显示 GDB 相关的信息。如:show version 显示GDB版本信息

1.6 bt: 显示当前进程的函数调用栈的情况;"up num":查看调用的详细信息;down:返回栈列表;l:显示详细代码信息;p:输出数值。

2. 添加全局断点(Add Exception BreakPoint):

2.1 添加步骤:

1. In the bottom-left corner of the breakpoints navigator, click the Add button. 
2. Choose Add Exception Breakpoint. 
3. Choose the type of exception from the Exception pop-up menu. 
4. Choose the phase of the exception handling process at which you want program execution to stop. 
5. Click Done.

2.2 使用场景:

程序因为SIGABRT而crash,想要定位到导致crash的行。

然后右键单击该断点选择move breakpoint to,选择User,即可以在所有项目中起作用

3. 添加符号断点(Add Symbolic BreakPoint):

3.1 断点执行的时机:Symbolic breakpoints stop program execution when a specific function or method starts executing

3.2 添加步骤:

1. Steps In the bottom-left corner of the breakpoint navigator, click the Add button.

2. Choose Add Symbolic Breakpoint. 
3. Enter the symbol name in the Symbol field. 
4. Click Done.

3.3 使用场景:

当想让系统在某个指定条件处中断时,设置相应的断点。

比如:

objc_exception_throw:在系统抛出异常处设置断点。

-[NSException raise]:

还可添加输入malloc_error_break的symbolic断点,以跟踪调试释放了2次的对象

4. 设置NSZombieEnabled、MallocStackLogging、NSAutoreleaseFreedObjectCheckEnabled、NSDebugEnabled:

4.1 设置方法:

1. Product->Edit Scheme...->Run...->EnvironmentVariables. 
2. add NSZombieEnabled,set the value with YES
3. add MallocStackLogging, set the value with YES.

4. add NSAutoreleaseFreedObjectCheckEnabled, set the value with YES.

5. add NSDebugEnabled, set the value with YES.

4.2 使用场景:

主要为了解决EXC_BAD_ACCESS问题,MallocStackLogging用来启用malloc记录(使用方式 malloc_history ${App_PID} ${Object_instance_addr})。

4.3 需要注意的问题

NSZombieEnabled只能在调试的时候使用,千万不要忘记在产品发布的时候去掉,因为NSZombieEnabled不会真正去释放dealloc对象的内存。

5. 重写respondsToSelector方法

5.1 实现方式


#ifdef _FOR_DEBUG_  
-(BOOL) respondsToSelector:(SEL)aSelector {  
printf("SELECTOR: %s ", [NSStringFromSelector(aSelector) UTF8String]);  
return [super respondsToSelector:aSelector];  
}  
#endif  
5.2 使用方法:
需要在每个object的.m或者.mm文件中加入上面代码(应该可以使用类属实现),并且在other c flags中加入-D  _FOR_DEBUG_(记住请只在Debug  Configuration下加入此标记)。这样当你程序崩溃时,Xcode的console上就会准确地记录了最后运行的object的方法。

参考文章:

1. Xcode GDB 调试:http://blog.csdn.net/ch_soft/article/details/7005998

2. XCode的一些调试技巧:http://blog.csdn.net/kesalin/article/details/7222153

3. About the Breakpoint  Navigator:http://developer.apple.com/library/mac/#recipes/xcode_help-breakpoint_navigator/articles/about_breakpoint_navigator.html#//apple_ref/doc/uid/TP40010433-CH6-SW1

4. 当程序崩溃的时候怎么办 part-1:http://article.ityran.com/archives/1006

5. 当程序崩溃的时候怎么办 Part-2:http://article.ityran.com/archives/1143

6. Memory Usage Performance  Guidelines:https://developer.apple.com/library/mac/#documentation /performance/Conceptual/ManagingMemory/ManagingMemory.html#//apple_ref/doc/uid/10000160-SW1

原文地址:https://www.cnblogs.com/woaixixi/p/4807811.html