iOS-如何修复message sent to deallocated instance与EXC_BAD_ACCESS类错误
2014-01-16 10:30
477 查看
转载地址 http://blog.csdn.net/foolenough/article/details/8090436
这两个错误都是由于往一个已经释放了的对象上发送了消息引起的,查找问题原因时有两个方法。
一. debug时打开NSZombieEnabled选项与Malloc Stack选项
NSZombieEnabled选项打开后,crash时能打印出具体的对象地址;Malloc Stack选项打开后,可以通过命令行打印出程序分配与释放空间的历史。两者相互结合,往往就能发现到底是什么对象被意外释放了。
打开选项Edit Schema->DIagnostics
查看Malloc Stack,例如出现以下错误时:
2012-10-19 16:49:06.699 Demo[8618:11603] *** -[ViewController setRouteInfo:]: message sent to deallocated instance 0x6e924f0
打开命令行工具Terminal,输入malloc_history 8618 0x6e924f0就可以看到
malloc_history Report Version: 2.0
。。。
按command+f,查找0x6e924f0的分配历史可以看到相应行
ALLOC 0x6e924f0-0x6e9259b [size=172]: thread_ac0aa2c0 |start | main | UIApplicationMain | GSEventRun | GSEventRunModal | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopRun | __CFRunLoopDoSource1
| __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ | PurpleEventCallback | _UIApplicationHandleEvent | -[UIApplication sendEvent:] | -[UIWindow sendEvent:] | -[UIWindow _sendTouchesForEvent:] | -[UIControl touchesEnded:withEvent:] | -[UIControl(Internal)
_sendActionsForEvents:withEvent:] | -[UIControl sendAction:to:forEvent:] | -[UIApplication sendAction:toTarget:fromSender:forEvent:] | -[UIApplication sendAction:to:from:forEvent:] | -[NSObject performSelector:withObject:withObject:] | -[HistoryViewController
enlargeMapButtonClicked:] | _objc_rootAlloc | +[NSObject allocWithZone:] | _objc_rootAllocWithZone | class_createInstance | calloc | malloc_zone_calloc
从而可以得出结论,0x6e924f0这个地址是在HistoryViewController enlargeMapButtonClicked方法里分配的。
再结合代码,应该不难找出具体的对象是什么。
二,使用Profile->Zombies
原文参见:http://stackoverflow.com/questions/11170614/viewcontroller-respondstoselector-message-sent-to-deallocated-instance-crash
打开Profile(Command+i),选择Zombies,然后让你的程序运行到崩溃的地方,可以看到如下
点击提示框中地址后面的向右按钮,可以看到
这时你可以看到所有该对象retain count的变化情况,以及引起retain count变化的函数。点击每行可以看到相应的详细代码
通过检查retain count的变化,可以找到与预想情况不一致的地方,从而修复该问题。
这两个错误都是由于往一个已经释放了的对象上发送了消息引起的,查找问题原因时有两个方法。
一. debug时打开NSZombieEnabled选项与Malloc Stack选项
NSZombieEnabled选项打开后,crash时能打印出具体的对象地址;Malloc Stack选项打开后,可以通过命令行打印出程序分配与释放空间的历史。两者相互结合,往往就能发现到底是什么对象被意外释放了。
打开选项Edit Schema->DIagnostics
查看Malloc Stack,例如出现以下错误时:
2012-10-19 16:49:06.699 Demo[8618:11603] *** -[ViewController setRouteInfo:]: message sent to deallocated instance 0x6e924f0
打开命令行工具Terminal,输入malloc_history 8618 0x6e924f0就可以看到
malloc_history Report Version: 2.0
。。。
按command+f,查找0x6e924f0的分配历史可以看到相应行
ALLOC 0x6e924f0-0x6e9259b [size=172]: thread_ac0aa2c0 |start | main | UIApplicationMain | GSEventRun | GSEventRunModal | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopRun | __CFRunLoopDoSource1
| __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ | PurpleEventCallback | _UIApplicationHandleEvent | -[UIApplication sendEvent:] | -[UIWindow sendEvent:] | -[UIWindow _sendTouchesForEvent:] | -[UIControl touchesEnded:withEvent:] | -[UIControl(Internal)
_sendActionsForEvents:withEvent:] | -[UIControl sendAction:to:forEvent:] | -[UIApplication sendAction:toTarget:fromSender:forEvent:] | -[UIApplication sendAction:to:from:forEvent:] | -[NSObject performSelector:withObject:withObject:] | -[HistoryViewController
enlargeMapButtonClicked:] | _objc_rootAlloc | +[NSObject allocWithZone:] | _objc_rootAllocWithZone | class_createInstance | calloc | malloc_zone_calloc
从而可以得出结论,0x6e924f0这个地址是在HistoryViewController enlargeMapButtonClicked方法里分配的。
再结合代码,应该不难找出具体的对象是什么。
二,使用Profile->Zombies
原文参见:http://stackoverflow.com/questions/11170614/viewcontroller-respondstoselector-message-sent-to-deallocated-instance-crash
打开Profile(Command+i),选择Zombies,然后让你的程序运行到崩溃的地方,可以看到如下
点击提示框中地址后面的向右按钮,可以看到
这时你可以看到所有该对象retain count的变化情况,以及引起retain count变化的函数。点击每行可以看到相应的详细代码
通过检查retain count的变化,可以找到与预想情况不一致的地方,从而修复该问题。
相关文章推荐
- iOS内存错误EXC_BAD_ACCESS的解决方法(message sent to deallocated instance)
- iOS内存错误EXC_BAD_ACCESS的解决方法(message sent to deallocated instance)
- iOS内存错误EXC_BAD_ACCESS的解决方法(message sent to deallocated instance)
- iOS内存错误EXC_BAD_ACCESS的解决方法(message sent to deallocated instance)
- iOS内存错误EXC_BAD_ACCESS的解决方法(message sent to deallocated instance)
- iOS内存错误EXC_BAD_ACCESS的解决方法(message sent to deallocated instance)
- iOS内存错误EXC_BAD_ACCESS的解决方法(message sent to deallocated instance)
- iOS内存错误EXC_BAD_ACCESS的解决办法(message sent to deallocated instance)
- iOS之message sent to deallocated instance与EXC_BAD_ACCESS类错误
- message sent to deallocated instance EXC_BAD_ACCESS 获取更多调试信息
- Xcode调试之exc_bad_access以及 message sent to deallocated instance
- 疑问:为ios 工程 勾选“enable zombie objects” 时,应用程序报错 *** -[UIImage release]: message sent to deallocated instance 0x20856fa0
- iOS 崩溃内存泄漏-[STGiftOperationManager retain]: message sent to deallocated instance 0x60400092a8c0
- [CALayer release]: message sent to deallocated instance iOS内存过度释放问题
- 如何在LLDB下排查message sent to deallocated instance问题
- [CALayer release]: message sent to deallocated instance iOS内存过度释放问题
- iOS [MTMeViewController scrollViewDidScroll:]: message sent to deallocated instance
- Xcode做ios开发时 message sent to deallocated instance问题的解决方法(gdb和lldb)
- 如何调试 message sent to deallocated instance 的问题
- 如何在LLDB下排查message sent to deallocated instance问题