重启游戏时EXC_BAD_ACCESS

时间:2012-02-26 11:20:13

标签: objective-c ios xcode exc-bad-access

现在试图解决这个错误很多年,希望有人可以提供帮助! : - )

我正在使用带有Objective-C的Cocos2d并拥有一个简单的游戏。我刚刚添加了一个“重启游戏”按钮,可以将您带回主菜单,以便您可以选择开始新游戏。问题是我重新开始游戏时会得到EXC_BAD_ACCESS。

   [self performSelector:@selector(drawone) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawtwo) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawthree) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawfour) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawfive) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawsix) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawseven) withObject:nil afterDelay:3];
     [self performSelector:@selector(draweight) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawnine) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawten) withObject:nil afterDelay:3];

我在'init'中运行上面的代码,这会导致游戏在第二次播放时崩溃

-(void)drawone {

但是,如果我要评论一些上述代码,例如

  [self performSelector:@selector(drawone) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawtwo) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawthree) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawfour) withObject:nil afterDelay:3];
    [self performSelector:@selector(drawfive) withObject:nil afterDelay:3];

     /* [self performSelector:@selector(drawsix) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawseven) withObject:nil afterDelay:3];
     [self performSelector:@selector(draweight) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawnine) withObject:nil afterDelay:3]; */

     [self performSelector:@selector(drawten) withObject:nil afterDelay:3];

然后我这次得到EXC_BAD_ACCESS - (void)drawfive(仍在第二次播放)

最终,评论出更多代码,例如

    [self performSelector:@selector(drawone) withObject:nil afterDelay:3];

    /*  [self performSelector:@selector(drawtwo) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawthree) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawfour) withObject:nil afterDelay:3];
    [self performSelector:@selector(drawfive) withObject:nil afterDelay:3];
    [self performSelector:@selector(drawsix) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawseven) withObject:nil afterDelay:3];
     [self performSelector:@selector(draweight) withObject:nil afterDelay:3];
     [self performSelector:@selector(drawnine) withObject:nil afterDelay:3]; */

     [self performSelector:@selector(drawten) withObject:nil afterDelay:3];

在获得EXC_BAD_ACCESS之前,它会进入第6次播放(你可以重启5次) - (void)drawone

EXC_BAD_ACCESS更改位置以及何时出现这一事实让我认为 - (void)drawone中没有问题,因为在某些情况下,它会毫无问题地运行。我听说使用僵尸来调试它可能会有所帮助:不幸的是,尝试后它没有。我是Objective-C的新手,所以如果这是一个简单的错误,我道歉。先感谢您。

这是drawone:

-(void)drawone
 {

onepos = (arc4random() % 10) + 1;


if(positions[(onepos - 1)] != 0)
{
    positions[(onepos - 1)] = 0;
    if(onepos == 1)
    {
        CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"

                                                             target:self
                                                           selector:@selector(oneclick:)];

        onem = [CCMenu menuWithItems:one, nil];
        onem.positionInPixels = ccp(53,420);
        [self addChild:onem];

    }
    else if(onepos == 2)
    {
        CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"

                                                             target:self
                                                           selector:@selector(oneclick:)];

        onem = [CCMenu menuWithItems:one, nil];
        onem.positionInPixels = ccp((53 + (106 * 1)),420);
        [self addChild:onem];


    }
    else if(onepos == 3)
    {
        CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"

                                                             target:self
                                                           selector:@selector(oneclick:)];

        onem = [CCMenu menuWithItems:one, nil];
        onem.positionInPixels = ccp((53 + (106 * 2)),420);
        [self addChild:onem];


    }
    else if(onepos == 4)
    {
        CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"

                                                             target:self
                                                           selector:@selector(oneclick:)];

        onem = [CCMenu menuWithItems:one, nil];
        onem.positionInPixels = ccp(53,300);
        [self addChild:onem];


    }
    else if(onepos == 5)
    {
        CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"

                                                             target:self
                                                           selector:@selector(oneclick:)];

        onem = [CCMenu menuWithItems:one, nil];
        onem.positionInPixels = ccp((53 + (106 * 1)),300);
        [self addChild:onem];


    }
    else if(onepos == 6)
    {
        CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"

                                                             target:self
                                                           selector:@selector(oneclick:)];

        onem = [CCMenu menuWithItems:one, nil];
        onem.positionInPixels = ccp((53 + (106 * 2)),300);
        [self addChild:onem];


    }
    else if(onepos == 7)
    {
        CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"

                                                             target:self
                                                           selector:@selector(oneclick:)];

        onem = [CCMenu menuWithItems:one, nil];
        onem.positionInPixels = ccp(53,180);
        [self addChild:onem];


    }
    else if(onepos == 8)
    {
        CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"

                                                             target:self
                                                           selector:@selector(oneclick:)];

        onem = [CCMenu menuWithItems:one, nil];
        onem.positionInPixels = ccp((53 + (106 * 1)),180);
        [self addChild:onem];


    }
    else if(onepos == 9)
    {
        CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"

                                                             target:self
                                                           selector:@selector(oneclick:)];

        onem = [CCMenu menuWithItems:one, nil];
        onem.positionInPixels = ccp((53 + (106 * 2)),180);
        [self addChild:onem];


    }
    else if(onepos == 10)
    {
        CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"

                                                             target:self
                                                           selector:@selector(oneclick:)];

        onem = [CCMenu menuWithItems:one, nil];
        onem.positionInPixels = ccp((53 + (106 * 1)),60);
        [self addChild:onem];


    }

}
else
{

    [self drawone];

}

}

我有全局变量:

int positions[] = {1,2,3,4,5,6,7,8,9,10};
int currentclick = 10; 
int onepos;
NSTimeInterval starttime;
CCMenu* onem;
CCMenu* twom;
CCMenu* threem;
CCMenu* fourm;
CCMenu* fivem;
CCMenu* sixm;
CCMenu* sevenm;
CCMenu* eightm;
CCMenu* ninem;
CCMenu* tenm;

同样,我是objective-c的新手,所以我可能做了一些非常愚蠢的事情

1 个答案:

答案 0 :(得分:0)

  

EXC_BAD_ACCESS改变位置及其出现的事实   让我假设 - (void)drawone没有问题   因为,在某些情况下,它会毫无问题地运行。

这可能是也可能不是。您的代码并不总是崩溃的事实并不意味着没有问题。 EXC_BAD_ACCESS表示您的代码正在某处访问错误的指针。听起来你的代码中有一个未初始化的指针,只要指针碰巧指向无效的地址,你的代码就会崩溃。如果指针恰好指向某个有效对象或包含nil(其中任何一个很容易因其他方法留在堆栈上的值而发生),那么您的代码不会崩溃。

调试此方法的方法是让代码崩溃,然后使用调试器查看崩溃发生时的代码。查看导致异常的行上使用的变量。如果崩溃发生在一些不属于你的方法(在框架中的某个地方),则备份堆栈,寻找第一个可以看到代码并检查代码的方法。