简体   繁体   English

为什么应用程序在iPhone模拟器中崩溃? 我怎样才能解决这个问题?

[英]Why is app crashing in iPhone simulator; how can I fix this?

I'm working on an app that uses a navigation controller. 我正在开发一个使用导航控制器的应用程序。 When I build, I get no errors or warnings. 构建时,没有任何错误或警告。 I've defined a method myMethod in FirstViewController.m file, then in viewDidLoad I call the method with [self myMethod]; 我在FirstViewController.m文件中定义了方法myMethod ,然后在viewDidLoad使用[self myMethod];调用该方法[self myMethod]; .

When I built and ran from the Console, I got nothing. 从控制台进行构建和运行时,我什么也没得到。 So I put NSLog() statements like so: 因此,我将NSLog()语句如下所示:

-(void) viewDidLoad {
NSLog(@"Entering viewDidLoad");
[self myMethod];
NSLog(@"Called myMethod");
[super viewDidLoad];
}

The Console never returns the second NSLog statement, leading me to believe that the app crashes while calling the method. 控制台从不返回第二条NSLog语句,这使我相信应用程序在调用该方法时崩溃。

I've tried this with the simulator set to iOS 4.0.1 as well as iOS 4.1. 我已经将模拟器设置为iOS 4.0.1和iOS 4.1进行了尝试。 I'm building against the iOS 4.1 SDK. 我正在针对iOS 4.1 SDK进行构建。 I really appreciate any help you can offer. 我非常感谢您可以提供的任何帮助。

Update: I ran the Debugger, which seems to show the problem being in myMethod . 更新:我运行了调试器,似乎表明问题出在myMethod I only make it through a few lines of code in that method before I receive "EXC_BAD_ACCESS". 在收到“ EXC_BAD_ACCESS”之前,我仅通过该方法中的几行代码来实现。 The lines of code are: 代码行是:

-(void)myMethod {
NSMutableArray *someStuff;
NSMutableArray *someMoreStuff;

stuffSections=[[NSMutableArray alloc] initWithObjects:@"Some Stuff",@"Some More Stuff",nil];

someStuff=[[NSMutableArray alloc] init];
someMoreStuff=[[NSMutableArray alloc] init];

[someStuff addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Item Name",@"name",@"Item Picture.png",@"picture",@"http://theurl.net/",@"url",@"1",@"itemCode",nil]];

But it appears that I can't get past this first attempt to add something to the someStuff array. 但是似乎我无法摆脱第一次向someStuff数组添加内容的尝试。

Have you cross checked the two MutableArrays? 您是否已交叉检查了两个MutableArrays? Do they have been allocated successfully? 是否已成功分配它们? You get " EXC_BAD_ACCESS " when the app got into a corrupted state that is usually due to a memory management issue. 当应用程序进入损坏状态(通常是由于内存管理问题)时,您将获得“ EXC_BAD_ACCESS ”。 I guess the arrays are not allocated. 我猜数组没有分配。

Try allocating this way. 尝试以这种方式分配。

NSMutableArray * someStuff = [[NSMutableArray alloc]initWithCapacity:3];
NSMutableArray * someMoreStuff = [[NSMutableArray alloc]initWithCapacity:3];

add a breakpoint there are check in the debug area and confirm whether they are being allocated successfully. 添加一个断点,在调试区域中进行检查,并确认是否已成功分配它们。 If there is a memory to each of these array, then they are allocated. 如果这些数组中的每一个都有一个内存,则将对其进行分配。

Looks like there is a problem in "myMethod". 看起来“ myMethod”中有问题。 If I had to guess, I would say you are trying to reference an object that was auto-released and not retained. 如果我不得不猜测,我会说您正在尝试引用自动释放但未保留的对象。

You're missing the alloc when initializing someStuff and someMoreStuff. 初始化someStuff和someMoreStuff时,您会丢失分配。 It should be 它应该是

   someStuff = [[NSMutableArray alloc] init];
   someMoreStuff = [[NSMutableArray alloc] init];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM