Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

ios - SKPhysicsBody malloc error

I created a subclass called Obstacle of SKSpriteNode and implement the physics body in the init:

-(id)initWithHeight:(NSInteger)height flipped:(BOOL)flipped
{
if (self = [super initWithImageNamed: @"obstacle.png"])
{
    [self setName: @"obstacle"];
    [self setSize: CGSizeMake(OBSTACLE_WIDTH, height)];
    [self setPosition: CGPointZero];
    // rotate if needed
    if (flipped)
    {
        [self runAction: [SKAction rotateByAngle: 3.14  duration: 0.0f]];

    }

    // physics
    SKPhysicsBody* pb = [SKPhysicsBody bodyWithRectangleOfSize: self.size];
    pb.dynamic = NO;
    pb.affectedByGravity = NO;
    [pb setCategoryBitMask: obstacleCategory];
    [pb setContactTestBitMask: playerCategory];
    [self setPhysicsBody: pb];
}
return self;
}

Then in my scene I have a method that spawns 2 obstacles at a time ever couple of seconds.

Obstacle *ob0 = [[Obstacle alloc] initWithHeight: h0 flipped: NO];
[bottom setPosition: CGPointMake(x0, y0)];

Obstacle *ob1 = [[Obstacle alloc] initWithHeight: h1 flipped: YES];
[top setPosition: CGPointMake(x1, y1)];

NSArray* objs = @[ob0, ob1];

for (Obstacle* o in objs)
{
    [self addChild: o];
    [o runAction: [SKAction
                        moveToX: -OBSTACLE_WIDTH duration: SPAWN_SPEED*2]
           completion:^{
               [o removeFromParent];
           }];
}

After running for a random period of time (between a few seconds and a few minutes), the app will crash with

malloc: *** error for object 0x16553120: pointer being freed was not allocated

The error gets thrown on the following line in the first chunk of code

SKPhysicsBody* pb = [SKPhysicsBody bodyWithRectangleOfSize: self.size];

I've tried moving the physics implementation out of the subclass and into the scene after the Obstacle is instantiated, but it throws the same error.

Edit:

The debugger gave me new insight to my problem:

*** Terminating app due to uncaught exception 'Cant add body, 
already exists in a world', reason: 'Cant add body <SKPhysicsBody> 
type:<Rectangle> representedObject:[<SKSpriteNode> name:'obstacle' 
texture:[<SKTexture> 'obstacle.png' (50 x 200)] position:{400, 446} 
size:{50, 244} rotation:0.00], already exists in a world'

Do SKPhysicsNodes need to have a unique name maybe?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found the solution! Because I was generating a random number for hight and subtracting that number from the height of the view for the other obstacle, sometimes the height was a negative value which would cause the error in creating the physics body. I hope this helps someone else down the line, cause I was banging my head over it all day


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...