I am writing a very simple iPhone app. I have a view with a number of objects.
right after initializing the array, it works fine. However, the contents seem to get corrupted when I call on the object later. I also get exec_bad_access even when I am within the range of the array.
Here is the initializer for the class with the object
Here is how I am adding to the array. Note when reading the contents of the array at this point everything is working fine.
Here is some time later when I try to print out the array in a function. The app crashes in the for-loop some of the time. But always outputs junk.
This is how im initializing the containing class. This is done in viewDidLoad
What am I doing wrong here. Let me know if there is anything else I should post.
right after initializing the array, it works fine. However, the contents seem to get corrupted when I call on the object later. I also get exec_bad_access even when I am within the range of the array.
Here is the initializer for the class with the object
Code:
-(SDGameState*) initWithMaxScore:(NSInteger) maxscore andMaxFace:(NSInteger) maxface{
self = [super init];
if (self) {
self->maxScore = maxscore;
self->currentScore = 0;
self->DI = [[Dice alloc] initWithMaxFace: maxface];
self->playerArray = [[[NSMutableArray alloc] init] retain];
self->lastRoll = 1;
}
return self;
}
Here is how I am adding to the array. Note when reading the contents of the array at this point everything is working fine.
Code:
(BOOL) addPlayer:(IPlayer*) player{
//TODO: Add Checks
NSLog(@"Adding Player");
[self->playerArray addObject:player];
return YES;
Here is some time later when I try to print out the array in a function. The app crashes in the for-loop some of the time. But always outputs junk.
Code:
-(NSInteger) rollDice{
NSInteger roll = [DI roll];
NSLog(@"%d",[playerArray count]);
for (int i = 0; i < [playerArray count]; i++) {
NSLog(@"Internal Player %d: %@", i+1,[[playerArray objectAtIndex:i] name]);
}
NSLog(@"%d",[playerArray count]);
//IPlayer *currentPlayer = [playerArray objectAtIndex:currentPlayerIndex];
[[playerArray objectAtIndex:currentPlayerIndex] addRoll];
[self addScore: roll];
if ([self isGameOver]) {
[[playerArray objectAtIndex:currentPlayerIndex]
addLoss];
}
//[playerArray replaceObjectAtIndex:currentPlayerIndex withObject:currentPlayer];
self->lastRoll = roll;
//[self beginNextTurn];
return roll;
}
This is how im initializing the containing class. This is done in viewDidLoad
Code:
//-- game variables
if (!gs) {
gs = [[[SDGameState alloc] initWithMaxScore: kMaxScore andMaxFace: kMaxFace] retain];
What am I doing wrong here. Let me know if there is anything else I should post.