1

Okay, so the title makes this seem like a pretty easy fix, but here is the problem. These objects are only manipulated through the loop below, and removing

[SKAction removeFromParent];

solves then problem, only the objects are still living as I can see the memory consumption growing over time, which then reduces frame-rate and also increases CPU.

I'm at a loss as to why this is causing a memory access exception. Thanks in advance.

for (int i=0;i<30; i++) {
    SKSpriteNode *particle = [SKSpriteNode spriteNodeWithColor:[SKColor purpleColor] size:CGSizeMake(3, 3)];
    particle.zPosition = 0;
    particle.position = position; // passed in through method call
    [self addChild:particle];

    SKAction *move = [SKAction moveByX:ScalarRandomRange(-25, 25) y:ScalarRandomRange(-25, 25) duration:0.1];
    SKAction *fadeOut = [SKAction fadeOutWithDuration:0.5];

    [particle runAction:[SKAction sequence:@[move,fadeOut]] withKey:@"explosion"];
}

Here is a screenshot of the location of the call- stack where the problem occurs.

Call Stack problem area

I've added the below code in the update method

-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
NSMutableArray *killList = [[NSMutableArray alloc]init];

for (SKNode *node in [self children]) {
    if ([node.name isEqualToString:@"particle"]) {
        if (!([node actionForKey:@"explosion"])) {
            [killList addObject:node];
        }
    }
}

for (SKNode *node in killList) {
    [node removeFromParent];
}

}

and it seems to have fixed the exception issue, but I still noticed the memory slightly climbing, I will need to investigate that in instruments.

This seems like a hackish way of fixing the problem though, as I should be able to use the

[SKAction removeFromParent];

in my sequence.

Derek Hewitt
  • 775
  • 8
  • 16
  • Does this help: http://stackoverflow.com/questions/22399278/sprite-kit-ios-7-1-crash-on-removefromparent?lq=1 – trojanfoe Jul 24 '14 at 08:19
  • Looks like that problem might be a little different than what I'm encountering. – Derek Hewitt Jul 24 '14 at 08:33
  • In what way is it different? – trojanfoe Jul 24 '14 at 08:36
  • Okay, so you're saying you receive EXC_BAD_ACCESS but when and where exactly? What does the call stack say? Does it really point to removeFromParent? Note that an increase in memory could be anything, you can only be certain that the actions or sprites remain in memory by looking at Instrument's live objects list. – CodeSmile Jul 24 '14 at 08:50
  • I've added a screenshot. – Derek Hewitt Jul 24 '14 at 16:03

1 Answers1

0

I understand that you want to check whether is your node still alive or not before calling removeFromAction method, this can be a solution:

particle.name = @"particle";

[particle runAction:[SKAction sequence:@[move, fadeOut, ([self childNodeWithName:@"particle"]?[SKAction removeFromParent]:nil)]]];

(I agree with LearnCocos2D that problem may have different reasons other than removeFromAction)