ゲームの難しい部分のコーディングを終えた後、いくつかのメモリ管理のバグを発見しました。
objects は、カスタム クラスを保持する NSMutableArray です。
- (void) spawnObjects
{
for (int index = 0; index < INITIAL_OBJECTS; index++)
{
[objects addObject:[[[MatchObject alloc] initWithImageNameID:(index % 3)] autorelease]];
[[objects objectAtIndex:index] setPosition:[GameLayer randomPoint]];
}
...
}
後でこの機能を使用します。
- (void) checkAllSprites
{
NSMutableArray *spritesToDelete = [NSMutableArray array];
for (int index = 0; index < [points count] - 1; index ++)
{
for (MatchObject *planetLike in objects)
{
CGPoint point1 = [[points objectAtIndex:index] CGPointValue];
CGPoint point2 = [[points objectAtIndex:index+1] CGPointValue];
if ([GameLayer lineIntersectsCircle:point1 :point2 :[planetLike position] :16.0f])
{
ParticleSystem *planetDeath = [ParticlePlanetDeath node];
planetDeath.texture = [[TextureMgr sharedTextureMgr] addImage:@"fire.pvr"];
planetDeath.position = [planetLike position];
[self addChild:planetDeath z:0 tag:2];
[spritesToDelete addObject:planetLike];
[self removeChild:planetLike cleanup:YES];
}
}
}
[objects removeObjectsInArray:spritesToDelete];
[spritesToDelete removeAllObjects];
}
最初の関数で自動解放しないと、アプリは正常に動作します。その場合、解放されたオブジェクト ([MatchObject position]) にアクセスしようとします。
どうしたの?!