简体   繁体   中英

application crashes after 2nd CCScene replacing

I'm creating a game, using cocos2d. Here is method, called when game creating:

- (void)createGame
{
    GameScene *newScene = [[GameScene alloc] initWithController:self]; //subclass of CCScene

    if ([CCDirector sharedDirector].runningScene)
        [[CCDirector sharedDirector] replaceScene:newScene];

    else 
        [[CCDirector sharedDirector] pushScene:newScene];

    scene = newScene;

    //some controllers for some layers of my scene
    box2d = [[Box2DController alloc] initWithParent:self];
    menu = [[MenuController alloc] initWithParent:self];
    controls = ([[ControlsController alloc] initWithParent:self]);

    self.controllers = [[NSArray alloc] initWithObjects:box2d, menu, controls, nil];

    //some object, contains some parameters. rizeEvent tells about some event to all controllers. In this case, it sends pointer to worldState to all of them.
    worldState = [[WorldState alloc] init];
    EventArgs *eventArgs1 = [[EventArgs alloc] initWithSender:self params:worldState];
    [self riseEvent:@"WorldStateUpdate" withArgs:eventArgs1];
}

I have a button, that destroys my world, and creates new one:

- (void)onExitPressedWithArgs:(EventArgs *)e
{
    [self destroyGame];
    [self createGame];
}

Here is 'destroyGame' method:

- (void)destroyGame
{   
    [box2d release];
    [menu release];
    [controls release];
    [scene release];
    [worldState release];

    box2d = nil;
    menu = nil;
    controls = nil;
    scene = nil;
    worldState = nil;

    [self.controllers release];
    self.controllers = nil;
}

So, I'm launching my applications:

  1. 'createGame' called
  2. pressing 'restart' button
  3. 'onExitPressedWithArgs' called
  4. 'destroyGame' and 'createGame' are called
  5. New world created, all goes fine
  6. pressing 'restart' button, 'onExitPressedWithArgs' called, 'destroyGame' and 'createGame' are called
  7. Application crashes.

It crashes always in different parts of cade, but always with "EXC_BAD_ACCESS" exception.

remove [self.controllers release]; from destroyGame method. As you are already calling self.controllers = nil; which will do the required job for you.

Similar to Rishi's suggestion, but there's a problem with the initial assignment as well.

1) Replace this:

self.controllers = [[NSArray alloc] initWithObjects:box2d, menu, controls, nil];

with this:

controllers = [[NSArray alloc] initWithObjects:box2d, menu, controls, nil];

2) And this:

[self.controllers release];
self.controllers = nil;

with:

[controllers release];
controllers = nil;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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