概述
编程中总会遇到各种各样的问题,这里对出现的一些问题进行汇总,不断补充
1. 野指针错误,使用了一个已经被释放了的指针,会导致程序崩溃。
如果一个对象注册了通知,但是在其dealloc的时候,并没有作remove操作,当通知中心向其发通知的时候,并不知道这个对象已经释放了,就会调用原指针地址的方法,导致出现野指针错误崩溃。
appTest[64158:fe03] -[UITableViewCell atestFun:]: unrecognized selector sent to instance 0x6bceb20
2012-06-08 17:09:14.813 appTest[64158:fe03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell atestFun:]: unrecognized selector sent to instance 0x6bceb20'
*** Call stack at first throw:
(
0 CoreFoundation 0x028bcbe9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x02a115c2 objc_exception_throw + 47
2 CoreFoundation 0x028be6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x0282e366 ___forwarding___ + 966
4 CoreFoundation 0x0282df22 _CF_forwarding_prep_0 + 50
5 Foundation 0x00d996c1 _nsnote_callback + 145
6 CoreFoundation 0x02894f99 __CFXNotificationPost_old + 745
7 CoreFoundation 0x0281433a _CFXNotificationPostNotification + 186
8 Foundation 0x00d8f266 -[NSNotificationCenter postNotificationName:object:userInfo:] + 134
9 Foundation 0x00d9b5a9 -[NSNotificationCenter postNotificationName:object:] + 56
又一次的执行,原因相同,但细节不同,这次是NSCFNumber
2012-06-09 10:14:39.823 appTest[65900:fe03] -[NSCFNumber atestFun:]: unrecognized selector sent to instance 0x6e639a0
2012-06-09 10:14:39.928 appTest[65900:fe03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber atestFun:]: unrecognized selector sent to instance 0x6e639a0'
*** Call stack at first throw:
典型的野指针问题
指针乱指,即原地址已经被分配给另外的对象使用了,再进行方法调用就会出错
如果指针指向了一个autorelease的对象,也可能会出现这个问题,需要注意。
多释放对象也会造成错误
2. 调用不存在的方法,这个问题在按钮的调用中出现较多,即调用了一个不存在的方法,一般是有编译告警的,编程的时候需要注意告警信息。也就是说,系统在调用的时候,并没有执行一个 [aObject responsToSelector:fun] 判断,通过crash来暴露问题提示给用户
又如:静态方法中调用了非静态方法,也会有告警,运行崩溃。值得注意的是,在静态方法中使用 performSelector:@selector(static_fun2) 调用另外一个静态方法static_fun2,是没有问题的,这里提出这个问题是因为performSelector看起来是一个非静态的方法。
3. 数组越界
4. 指针的使用
5. 使用一些系统类的时候,如UITableViewController 的时候,其tableView可能被释放,不好控制,在出现问题的时候需要留意
6. 内存泄露,如果一个方法里存在内存泄露的问题,并且这个方法会被频繁调用,当泄露的内存到了一定大小的时候,会出现问题,所以必须关注内存泄露的问题
----
//7-9
调试
po obj_name
查看一个对象的值(内容),po means print object
gdb的调试命令都是可以使用的,在有些时候,可以根据地址来进行查看
如
p *((Class_name *)0x2322c2)
(gdb)p *((UIButton*)0x6d98ce0)
$1 = {
<UIControl> = {
<UIView> = {
<UIResponder> = {
<NSO
_backgroundView = 0x6dc0210,
_imageView = 0x0,
_titleView = 0x6dc00c0,
_initialized = 1 '