您的位置:首页 > 移动开发 > IOS开发

iOS指南系列:如何解决奔溃问题-关于内存访问

2012-04-13 22:20 916 查看


第一个内存错误

我们的第一个问题,应该有解决了。再次运行程序。哎呦,它崩溃,在同一行,现在只显示一个EXC_BAD_ACCESS错误。这意味着应用程序有一个内存管理的问题。





内存相关的崩溃的来源往往是很难确定,因为内存破坏可能已在更早前完成得。如果故障的代码一块内存结构破坏,这一结果可能不会出现,直到很久以后,在一个完全不同的地方,程序奔溃了。

其实,而在所有测试中这个错误可能永远不会出现, 只有客户的设备,其丑陋的方式。你真不希望这样的事情发生!

然而,有时候这种特殊的崩溃是很容易解决。如果你看一下在源代码编辑器时,Xcode已警告有关此行的所有沿。看到左边行号旁边的黄色三角形?这表明编译器警告。如果你点击黄色三角形时,Xcode应弹出“修复”这样的建议:





代码初始化给它一个对象的名单,这些名单应该被终止使用无NSArray对象,哨兵的警告中提到。但是,却没有这样做,现在的NSArray迷糊。它尝试读取不存在的对象,导致应用程序崩溃。

这一个错误,你真的不应该犯,特别是因为Xcode中已经警告你。修复代码,通过添加nil如下(或者,您可以简单地选择“修复”菜单选项):

viewController.list = [NSArray arrayWithObjects:@"One", @"Two", nil];


“This class is not key value coding-compliant”

再次运行程序,看到这个项目有其他一些有趣的错误。知道什么?再次崩溃于main.m.由于异常断点仍处于启用状态,但我们没有看到任何应用程序的源代码强调,这次奔溃的真正没有发生任何的应用程序的源代码。调用堆栈证实:这些方法都属于应用程序,除main():





如果你看一下通过自上而下的方法名,有NSObject的键 - 值编码的一些东西。下面是一个调用到[UIRuntimeOutletConnection连接]。我不知道那是什么,但它看起来像有东西做连接的outlet,和view有些相关。下面是谈方法,从stack加载来分析,已经为您提供了一些线索。

然而,在Xcode的调试窗格没有便捷的错误消息。这是因为还没有被抛出异常。异常断点暂停程序之前,它会告诉你异常的原因。有时你会得到部分的错误消息异常断点的启用,有时你不能。

To see the full error message, click the “Continue Program Execution” button in the debugger toolbar: 你 可能需要多次点击,然后才最后抛出错误信息





You may need to click it more than once, but then you’ll get the error message:

Problems[14961:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<MainViewController 0x6b3f590> setValue:forUndefinedKey:]: this class is not
key value coding-compliant for the key button.'
*** First throw call stack:
(0x13ba052 0x154bd0a 0x13b9f11 0x9b1032 0x922f7b 0x922eeb 0x93dd60 0x23091a 0x13bbe1a
0x1325821 0x22f46e 0xd6e2c 0xd73a9 0xd75cb 0xd6c1c 0xfd56d 0xe7d47 0xfe441 0xfe45d
0xfe4f9 0x3ed65 0x3edac 0xfbe6 0x108a6 0x1f743 0x201f8 0x13aa9 0x12a4fa9 0x138e1c5
0x12f3022 0x12f190a 0x12f0db4 0x12f0ccb 0x102a7 0x11a9b 0x2872 0x27e5)
terminate called throwing an exception

像以前一样,你可以忽略底部的数字。他们所代表的调用堆栈,但你已经有了,一个更方便 - 可读! - 调试左侧导航中的格式。

The interesting bits are:

NSUnknownKeyException

MainViewController

“this class is not key value coding-compliant for the key
button

异常名称 ,NSUnknownKeyException,往往是一个很好的指标,什么是错的。它会告诉你有一个“未知的键”的地方。某处显然是MainViewController,键被命名为“button”

正如我们已经建立,这一切发生了,而加载的stack。应用程序使用,而不是stack的storyboard,但内部的故事情节是只的笔尖收集,所以它必须是一个错误的scenes,查看 MainViewController:





In the Connections Inspector, you can see that the UIButton in the center of the view controller is connected to MainViewController’s “button” outlet. So the storyboard/nib refers to an outlet named “button,” but according to the error message it can’t find
this outlet.

Have a look at MainViewController.h:

@interface MainViewController : UIViewController

@property (nonatomic, retain) NSArray *list;
@property (nonatomic, retain) IBOutlet UIButton *button;

- (IBAction)buttonTapped:(id)sender;

@end

The @property definition for the “button” outlet is there, so what’s the problem? If you’ve been paying attention to the compiler warnings, you may have figured it out already.

If not, check out MainViewController.m/s
@synthesize list. Do you see the problem now?

The code doesn’t actually @synthesize the button property. It tells MainViewController that it has a property named “button,” without providing it with a backing instance variable and getter and setter methods (which is what @synthesize does).

Add the following to MainViewController.m below
the existing @synthesize line to fix this issue:

@synthesize button = _button;

Now the app should no longer crash when you run it!

Note: The
error “this class is not key value coding-compliant for the key XXX” usually occurs when loading a nib that refers to a property that doesn’t actually exist. This usually happens when you remove an outlet property from your code but not from the connections
in the nib.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: