Objective-C:应用程序仅运行一次,但编译完美

发布于 2024-12-23 00:08:39 字数 2947 浏览 1 评论 0原文

我创建了一个可以正常编译的应用程序,但是当我尝试再次玩时,模拟器崩溃了,我不明白为什么。我的代码中的相关部分如下:

iFocus2AppDelegate.m


- (void) flipToGameScreen:(NSInteger *)aMode aLevel:(NSInteger*)aLevel 
{
    NSInteger *myMode = aMode;
    NSInteger *myLevel = aLevel;

    GameScreenViewController *aGameScreenView = [[GameScreenViewController alloc] initWithNibName:@"GameScreen" mode:myMode level:myLevel bundle:nil];
    [self setGameScreenViewController:aGameScreenView];
    [aGameScreenView release];
    gameScreenViewController.view.frame =[[UIScreen mainScreen] applicationFrame];

    //animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:window cache: YES];
    [viewController.view removeFromSuperview];
    [self.window addSubview:[gameScreenViewController view]];
    [UIView commitAnimations];      

 }


GameScreenViewController.m


- (id)initWithNibName:(NSString *)nibNameOrNil mode:(NSInteger *)myMode level:(NSInteger *)myLevel bundle:(NSBundle *)nibBundleOrNil 
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        // Custom initialization.
        self.selectedMode = myMode;
        self.selectedLevel = myLevel;

    }
    return self;
}

- (void) finished
{   
    [myTimer invalidate];
    myTimer = nil;
    [myTimer release];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"GameOver"
                 message:@"Do Something"
                 delegate:self
                 cancelButtonTitle:nil
                 otherButtonTitles:nil];
    [alert addButtonWithTitle:@"Play Again"];
    [alert addButtonWithTitle:@"Show HiScores"];
    [alert addButtonWithTitle: @"Setup New Game"];

    [alert show];
    [alert release];
}

- (void) alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        //PlayAgain
        iFocus2AppDelegate *mainDelegate = (iFocus2AppDelegate *)[[UIApplication sharedApplication] delegate];
        [mainDelegate flipToGameScreen:selectedMode aLevel:selectedLevel];
        [iFocus2AppDelegate release];

    }
    if  (buttonIndex == 1)
    {
        //scores
        iFocus2AppDelegate *mainDelegate = (iFocus2AppDelegate *)[[UIApplication sharedApplication] delegate];
        [mainDelegate flipToHiScores];
        [iFocus2AppDelegate release];
    }
    if  (buttonIndex == 2)
    {
        //setings
        iFocus2AppDelegate *mainDelegate = (iFocus2AppDelegate *)[[UIApplication sharedApplication] delegate];
        [mainDelegate flipToGameSettings];
        [iFocus2AppDelegate release];
    }
}

我粘贴了这两部分,因为我认为 GameScreenViewController 一定有问题,因为它在第二次进入此屏幕时崩溃(任何其他屏幕翻转(即从警报到HiScores)工作。请随意询问您可能需要的任何其他代码,我们将不胜感激

(已编辑):根据要求,这就是控制台所说的...

(再次编辑):之前粘贴的代码不是。实数部分;尽管我已阅读所有答案,但崩溃时我在控制台中返回的唯一代码是“GDB:程序收到信号:“EXC_BAD_ACCESS”在构建和调试中。

I have created an app that compiles ok but, when I try to play again, the simulator crashes and I can't figure out why. The relevant parts in my code are as follows:

iFocus2AppDelegate.m


- (void) flipToGameScreen:(NSInteger *)aMode aLevel:(NSInteger*)aLevel 
{
    NSInteger *myMode = aMode;
    NSInteger *myLevel = aLevel;

    GameScreenViewController *aGameScreenView = [[GameScreenViewController alloc] initWithNibName:@"GameScreen" mode:myMode level:myLevel bundle:nil];
    [self setGameScreenViewController:aGameScreenView];
    [aGameScreenView release];
    gameScreenViewController.view.frame =[[UIScreen mainScreen] applicationFrame];

    //animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:window cache: YES];
    [viewController.view removeFromSuperview];
    [self.window addSubview:[gameScreenViewController view]];
    [UIView commitAnimations];      

 }


GameScreenViewController.m


- (id)initWithNibName:(NSString *)nibNameOrNil mode:(NSInteger *)myMode level:(NSInteger *)myLevel bundle:(NSBundle *)nibBundleOrNil 
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        // Custom initialization.
        self.selectedMode = myMode;
        self.selectedLevel = myLevel;

    }
    return self;
}

- (void) finished
{   
    [myTimer invalidate];
    myTimer = nil;
    [myTimer release];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"GameOver"
                 message:@"Do Something"
                 delegate:self
                 cancelButtonTitle:nil
                 otherButtonTitles:nil];
    [alert addButtonWithTitle:@"Play Again"];
    [alert addButtonWithTitle:@"Show HiScores"];
    [alert addButtonWithTitle: @"Setup New Game"];

    [alert show];
    [alert release];
}

- (void) alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        //PlayAgain
        iFocus2AppDelegate *mainDelegate = (iFocus2AppDelegate *)[[UIApplication sharedApplication] delegate];
        [mainDelegate flipToGameScreen:selectedMode aLevel:selectedLevel];
        [iFocus2AppDelegate release];

    }
    if  (buttonIndex == 1)
    {
        //scores
        iFocus2AppDelegate *mainDelegate = (iFocus2AppDelegate *)[[UIApplication sharedApplication] delegate];
        [mainDelegate flipToHiScores];
        [iFocus2AppDelegate release];
    }
    if  (buttonIndex == 2)
    {
        //setings
        iFocus2AppDelegate *mainDelegate = (iFocus2AppDelegate *)[[UIApplication sharedApplication] delegate];
        [mainDelegate flipToGameSettings];
        [iFocus2AppDelegate release];
    }
}

I pasted these two pieces as I think there must be something wrong specially with GameScreenViewController, as it crashes when it enters this screen for the second time (any other screen flipping (i.e., from alert to HiScores) works. Feel free to ask for any other piece of code you may need, any help will be appreciated.

(Edited): as requested, here's what the console says...

(Edited again): previous pasted code wasn't the real part; even though I have read through all answers, the only code I get back in the console when it crashes is "GDB: Program received signal: "EXC_BAD_ACCESS" when in Build&Debug.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此生挚爱伱 2024-12-30 00:08:39

某处,某些东西正在尝试创建 GameScreenViewController,但选择器 initWithNibName:mode:level:bundle:bundle: 部分被省略。根据回溯,它似乎位于 iFocus2AppDelegate 的 flipToGameScreen:aLevel: 方法中,尽管我在那里没有看到它。如果使用旧版本的类,您可以尝试清理项目。

Somewhere, something is trying to create a GameScreenViewController, but the bundle: part of the selector initWithNibName:mode:level:bundle: is being omitted. Based on the backtrace, it would appear to be in your iFocus2AppDelegate's flipToGameScreen:aLevel: method, though I don't see it there. You might try cleaning your project in case an old version of the class is being used.

傲世九天 2024-12-30 00:08:39

感谢 David Rönnqvist 和主要是查克,我开始进一步关注控制台,以及“EXC_BAD_ACCESS”消息。

问题不是一个错误的调用,而是 NSMutableArray 的错误清理,从而导致内存泄漏呈指数级增加,因为我的代码的某些部分有一些大循环。我以为“release”指令会清理我创建的任何东西,但似乎不会(我必须说,我有更多的 Android 和 Windows Phone 编程经验,我正在尝试将我的解决方案翻译成 iPhone)。

因此,通过在代码开头添加 [myArray RemoveAllObjects],我已经解决了问题。

正如甘地所说,你为我提供的是一根钓鱼竿,而不是一条鱼。非常感谢前面提到的人,以及杰克·劳伦斯的努力。

Thanks to David Rönnqvist & Chuck mainly, I started to pay further attention to the console, and to that "EXC_BAD_ACCESS" message.

The problem was not a bad call but a bad cleaning of a NSMutableArray, thus creating a memory leak that increased exponentially since I've got a few large loops in some part of my code. I thought that the "release" instruction would clean up anything I created, but it seems not (I must say, I have more programming experience for Android and Windows Phone, I'm trying to translate my solutions into iPhone).

Thus, by adding [myArray RemoveAllObjects] at the beginning of my code, I have solved the problem.

As Ghandi said, you've provided me with a fishing rod rather than a fish. Thanks a lot to the formerly mentioned, as well as to Jack Lawrence for his effort.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文