malloc错误:未分配释放的指针

时间:2013-11-03 23:03:08

标签: objective-c arrays pointers malloc

当对象不存在时,如何找到指针指向的对象? 请继续阅读。这不是一个愚蠢的问题。 我已经在这个游戏中创建了太多的数组,现在有点难以找到我留下的数据而不计算对象。 我知道错误正在显示,因为对象不再存在,但有没有人知道找到确切的变量(在数组中),甚至是什么数组导致问题的好方法?我在这里寻找调试解决方案。 我看到其他类似的问题,但无法找到解决问题的答案。

导致错误的函数如下所示,但请记住我不是要修复函数,而是要正确调试并找到问题。

-(void)handleContact:(SKPhysicsContact*)contact {
NSLog(@"Handling contact");
// Ensure you haven't already handled this contact and removed its nodes
if (!contact.bodyA.node.parent || !contact.bodyB.node.parent) return;

NSArray* nodeNames = @[contact.bodyA.node.name, contact.bodyB.node.name];
if ([nodeNames containsObject:kPlayerName] && [nodeNames containsObject:kEnemyWeaponName]) {
    // ENEMY BULLET HITS YOU
    [self adjustShipHealthBy:-0.20f];
    if (self.playerHealth <= 0.0f) {
        //2
        [contact.bodyA.node removeFromParent];
        [contact.bodyB.node removeFromParent];
    } else {
        //3
        SKNode* ship = [self childNodeWithName:kPlayerName];
        ship.alpha = self.playerHealth;
        if (contact.bodyA.node == ship) [contact.bodyB.node removeFromParent];
        else [contact.bodyA.node removeFromParent];
    }
} else if ([nodeNames containsObject:kEnemyName] && [nodeNames containsObject:kPlayerWeaponName]) {
    // YOU HIT THE ENEMY
    //[self runAction:[SKAction playSoundFileNamed:@"InvaderHit.wav" waitForCompletion:NO]];

    NSString *burstPath = [[NSBundle mainBundle] pathForResource:@"MySpark" ofType:@"sks"];
    SKEmitterNode *explosion = [NSKeyedUnarchiver unarchiveObjectWithFile:burstPath];
    explosion.numParticlesToEmit = 100;
    explosion.position = contact.bodyA.node.position;
    [self addChild:explosion];

    [contact.bodyA.node removeFromParent];
    [contact.bodyB.node removeFromParent];
    [self adjustScoreBy:100];

}else if ([nodeNames containsObject:kPlayerName] && [nodeNames containsObject:kHealthPackName]){
    // GET HEALTH PACK
    NSLog(@"=========== HEALTH ============");
    [self adjustShipHealthBy:0.40f];
    NSString *healthPath = [[NSBundle mainBundle] pathForResource:@"HealthKit" ofType:@"sks"];
    SKEmitterNode *healthGain = [NSKeyedUnarchiver unarchiveObjectWithFile:healthPath];
    healthGain.numParticlesToEmit = 5;
    healthGain.position = contact.bodyA.node.position;
    [self addChild:healthGain];

    SKNode *thatHealthNode = [self childNodeWithName:kHealthPackName];
    [thatHealthNode removeFromParent];
    }

}

0 个答案:

没有答案